0% found this document useful (0 votes)
15 views

CC 102 - Fundamental Programming Lecture 3

This document discusses Java variables, data types, and input/output. It defines different variable types like String, int, float, char, and boolean. It also covers variable declarations and assignments, type casting, and displaying variable values using println.

Uploaded by

cleofe calo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

CC 102 - Fundamental Programming Lecture 3

This document discusses Java variables, data types, and input/output. It defines different variable types like String, int, float, char, and boolean. It also covers variable declarations and assignments, type casting, and displaying variable values using println.

Uploaded by

cleofe calo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

JAVA PROGRAMMING

Lecture 3 : Variable , data type , input/output


Java Syntax

Review
✓ The main method It is required and you will see it in every Java
main() program

public static void main(String[] args)

✓ println() method This method is to print a line of text to the


screen

System.out.println("Hello World");
Java Variables
Variables are ❑String - stores text, such as "Hello". String values
containers for are surrounded by double quotes
storing data ❑int - stores integers (whole numbers), without
values. decimals, such as 123 or -123
❑float - stores floating point numbers, with decimals,
such as 19.99 or -19.99
different types ❑char - stores single characters, such as 'a' or 'B'.
of variables Char values are surrounded by single quotes
❑boolean - stores values with two states: true or
false
Java Variables declarations
To create a variable, Example
you must specify the Create a variable called name of
type and assign it a type String and assign it the value "John":
value:

String name = "John";


Syntax

type variable = value;


Java Variables declarations
To create a variable, Example
you must specify the Create a variable called name of
type and assign it a type String and assign it the value "John":
value:

String name = "John";


Syntax

type variable = value;


Java Variables declarations
To create a variable, Example
you must specify the Create a variable called name of
type and assign it a type String and assign it the value "John":
value:

String name = "John";


Syntax

type variable = value;


Create a variable called myNum of type int and assign it the value 15:

Java Variables declarations


To create a variable, Example
you must specify the Create a variable called name of
type and assign it a type String and assign it the value "John":
value:

String name = "John";


Syntax
Display the variable value
type variable = value; System.out.println(name);
Create a variable called myNum of type int and assign it the value 15:

Java Variables declarations


To create a variable, Example
you must specify the You can also declare a variable without
type and assign it a assigning the value, and assign the value
later
value:
int num;
Syntax num = 15;

Display the variable value


type variable = value; System.out.println(num);
Create a variable called myNum of type int and assign it the value 15:

Java Variables declarations


Note that if you
assign a new value to Example
an existing variable, it
will overwrite the Change the value of num from 7 to 3:
previous value

int num = 7 ;
num = 3;

Display the variable value


System.out.println(num);
Java Variables declarations
Example
Other Types
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D'; // Character
boolean myBool = true; // Boolean
String myText = "Hello"; // String

Display the variable value


System.out.println(myFloatNum);
System.out.println(myLetter);
Primitive Data Types
A primitive data type specifies the size and type of variable values, and
it has no additional methods.
There are eight primitive data types in Java:
Primitive Data Types
A primitive data type specifies the size and type of variable values, and
it has no additional methods.
There are eight primitive data types in Java:
Numbers
Primitive number types are divided into two groups:

Integer types stores whole numbers, positive or negative


(such as 123 or -456), without decimals. Valid types
are byte, short, int and long. Which type you should use,
depends on the numeric value.

Floating point types represents numbers with a fractional


part, containing one or more decimals. There are two
types: float and double.
Java Type Casting
Type casting is when you assign a value of one primitive data type to
another type.

In Java, there are two types of casting:

•Widening Casting (automatically) - converting a smaller type to a larger


type size
byte -> short -> char -> int -> long -> float -> double

•Narrowing Casting (manually) - converting a larger type to a smaller


size type
double -> float -> long -> int -> char -> short -> byte

You might also like