By
Dr. Yasser Abdelhamid
Variables
Data types
http://www.w3schools.com/
A variable is a container which holds the
value while the Java program is executed.
It has name, data type, and value.
int data=10;
Names can contain letters, digits,
underscores, and dollar signs
Names must begin with a letter
Names can also begin with $ and _
Names cannot contain whitespace
Names are case sensitive ("myVar" and
"myvar" are different variables)
Reserved words (like Java keywords, such as
int or boolean) cannot be used as names
Variable names are preferred to be self-
declarative (has a meaning for the reader).
It is a tradition to use Camel Case for naming
variables with multiple words.
Variable name / Comments
X Not self declarative
4Times Starts with a number
A$$$ Not self declarative
myAddress
_test
$1 Not self declarative
myNewDevice
First One White space included
Last1
A variable can hold a specific type of data.
There are different types of data, the
following are the commonly used data types
in java.
String - stores text, such as "Hello". String values
are surrounded by double quotes
int - stores integers (whole numbers), without
decimals, such as 123 or -123
float - stores floating point numbers, with
decimals, such as 19.99 or -19.99
double- stores floating point numbers, with
decimals, using a double precision.
char - stores single characters, such as 'a' or 'B'.
Char values are surrounded by single quotes
boolean - stores values with two states: true or
false
Primitive data types - includes
byte,
short,
int,
long,
float,
double,
boolean and
char
Non-primitive
data types - such as String,
Arrays and Classes
Syntax
<data type> <variableName> ;
Example:
int myAge;
double employeeSalary;
String employeeName;
boolean married;
int x,y,z;
Syntax:
<data type> <variableName> = <value> ;
Example:
int myAge = 34;
double employeeSalary = 2336.50;
String employeeName = “Mohamed”;
boolean married = true;
char bloodType = ‘A’;
int x=9,y=6;
x = y = 77;
A primitive data type specifies the size
and type of variable values, and it has no
additional methods.
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal
digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal
digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
Primitive types are predefined (already
defined) in Java. Non-primitive types are
created by the programmer and is not
defined by Java (except for String).
Non-primitive types can be used to call
methods to perform certain operations,
while primitive types cannot.
A primitive type has always a value, while
non-primitive types can be null.
A primitive type starts with a lowercase
letter, while non-primitive types starts
with an uppercase letter.
Widening Casting (automatically) -
converting a smaller type to a larger type
size
byte -> short -> char -> int -> long -> float -> double
public class MyClass {
public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt;
// Automatic casting: int to double
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}
Narrowing Casting (manually) - converting
a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
public class MyClass{
public static void main(String[] args) {
double myDouble = 9.78d;
int myInt = (int) myDouble;
// Manual casting: double to int
System.out.println(myDouble); // Outputs 9.78
System.out.println(myInt); // Outputs 9
}
}
What is the output?
public class FundamentalEx10 {
public static void main(String args[])
{
int i=260;
byte b=(byte) i;
System.out.println("b="+b);
}
}
Here we are trying to type cast a larger
variable(int) into a smaller variable(byte).
So, in this case, java calculates modulo of
larger variable by the range of smaller
variable.
Our byte range is -128 to 127.
The range is 128+127+1 (for the 0 )
So final result would be 260 % 256 i.e. 4
When we prefix numerical values with 0x
or 0X, Java treats them as a hexadecimal
integer literal representation
What is the output?
public class FundamentalEx3 {
public static void main(String args[])
{
int c=0x12;
int d=0x1E;
int e=0X1F;
System.out.println("c="+c);//18
System.out.println("d="+d);//30
System.out.println("e="+e);//31
}
}
When we prefix numerical values with 0b
or 0B, Java treats them as a binary integer
literal representation
What is the output?
public class FundamentalEx3 {
public static void main(String args[])
{
int c=0b101;
int d=0B110;
int e=0b1111;
System.out.println("c="+c);//5
System.out.println("d="+d);//6
System.out.println("e="+e);//15
}
}
When we prefix numerical values with 0
(zero), Java treats them as a octal integer
literal representation
What is the output?
public class FundamentalEx3 {
public static void main(String args[])
{
int c=023;
int d=044;
int e=017;
System.out.println("c="+c);//19
System.out.println("d="+d);//36
System.out.println("e="+e);//15
}
}