Java_3
Java_3
DATA TYPE
By : Kapil Manchandani
TABLE OF CONTENT
1 : What is Variable ?
2: Types of Variable
Syntax
type variableName = value;
Where type is one of Java's types (such as int or String),
and variableName is the name of the variable (such as x or name).
The equal sign is used to assign values to the variable.
To create a variable that should store text, look at the following example:
Example
Create a variable called name of type String and assign it the value
"John":
String name = "John"; System.out.println(name);
EXAMPLES
int myNum = 5;
float myFloatNum = 5.99f;
To declare more than one variable of the same type, you can use a
comma-separated list:
Example
Instead of writing:
int x = 5;
int y = 6;
int z = 50;
System.out.println(x + y + z);
You can simply write:
int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
JAVA IDENTIFIERS
}//end of class
Java Variable Example: Add Two Numbers
public class Simple{
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}
DATA TYPES IN JAVA
Data types specify the different sizes and values that can be stored in the
variable. There are two types of data types in Java:
Primitive data types: The primitive data types include boolean, char,
byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types include Classes,
Interfaces, and Arrays.
Data types are divided into two groups:
Primitive data types -
includes byte, short, int, long, float, double, boolean and char
Non-primitive data types - such as String, Arrays and Classes (you will
learn more about these in a later chapter)
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:
EXERCISES
myNum = 9;
myFloatNum = 8.99f;
myLetter = 'A';
myBool = false;