Chap3-Basics of Java
Chap3-Basics of Java
A variable is a container which holds the value while the Java program is executed. Variable is
a name of memory location. There are two types of data types in Java: primitive and non-
primitive.
Primitive Data Type: such as boolean, char, int, short, byte, long, float, and double.
When a primitive data type is stored, it is the stack that the values will be assigned. When a
variable is copied then another copy of the variable is created and changes made to the
copied variable will not reflect changes in the original variable.
Ex. Demo
Non-Primitive Data Type or Object Data type: such as String, Array, etc.
When the reference variables will be stored, the variable will be stored in the stack and the
original object will be stored in the heap. In Object data type although two copies will be
created they both will point to the same variable in the heap, hence changes made to any
variable will reflect the change in both the variables
Ex. Demo
Example
Type Description Default Size Literals Range of values
twos-
8
complement 0 (none) -128 to 127
bits
byte integer
twos-
16
complement 0 (none) -32,768 to 32,767
bits
short integer
twos- -2,147,483,648
32
complement 0 -2,-1,0,1,2 to
bits
int intger 2,147,483,647
to
integer
9,223,372,036,854,775,807
1.23e100f , -
IEEE 754
32 1.23e-
floating 0.0 upto 7 decimal digits
bits 100f , .3f ,3.1
point
float 4F
Unicode System
Unicode is a universal international standard character encoding that is capable of representing
o ASCII (American Standard Code for Information Interchange) for the United States.
o ISO 8859-1 for Western European Language.
o KOI-8 for Russian.
o GB18030 and BIG-5 for Chinese, etc
Problem
This caused two problems:
1. A particular code value corresponds to different letters in the various language standards.
2. The encodings for languages with large character sets have variable length. Some common characte
encoded as single bytes, other require two or more byte.
Solution
To solve these problems, a new language standard was developed i.e. Unicode System.
In unicode, character holds 2 byte, so java also uses 2 byte for characters.
lowest value:\u0000
highest value:\uFFFF
Operators in Java
Operator in Java is a symbol that is used to perform operations
Arithmetic multiplicative * / %
additive + -
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ? :
When changes are made Change does not reflect in Changes reflected in the
in the copied variable the original ones. original ones.
Example byte, short, int, long, float, array, string class, interface
Properties Primitive data types Objects
Java Identifiers
In Java, identifiers are used for identification purposes. Java Identifiers can
be a class name, method name, variable name, or label.
A variable declared inside the body of the method is called local variable. You
can use this variable only within that method and the other methods in the
class aren't even aware that the variable exists. A local variable cannot be
defined with "static" keyword.
Converting a lower data type into a higher one is called widening type casting. It is
also known as implicit conversion or casting down. It is done automatically. It is
safe because there is no chance to lose data.
Converting a higher data type into a lower one is called narrowing type casting. It is also
known as explicit conversion or casting up. It is done manually by the programmer. If we do
not perform casting, then the compiler reports a compile-time error.
double -> float -> long -> int -> char -> short -> byte