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

Data Types in Java and Opearator and Separators

The document provides an overview of data types in Java, categorizing them into primitive and non-primitive types. It details the eight primitive data types, their default values, sizes, and ranges, as well as explaining Java tokens, including reserved keywords, identifiers, literals, and operators. Additionally, it covers the Unicode system and how Java uses it for character representation.

Uploaded by

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

Data Types in Java and Opearator and Separators

The document provides an overview of data types in Java, categorizing them into primitive and non-primitive types. It details the eight primitive data types, their default values, sizes, and ranges, as well as explaining Java tokens, including reserved keywords, identifiers, literals, and operators. Additionally, it covers the Unicode system and how Java uses it for character representation.

Uploaded by

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

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:

1. Primitive data types: The primitive data types


include boolean, char, byte, short, int, long, float and
double.
2. Non-primitive data types: The non-primitive data
types include Classes, Interfaces, and Arrays.
Java Primitive Data Types

In Java language, primitive data types are the building


blocks of data manipulation. These are the most basic
data types available in Java language.
Java is a statically-typed programming language. It means,
all variables must be declared before its use. That is why we need
to declare variable's type and name.

There are 8 types of primitive data types:

o boolean data type


o byte data type
o char data type
o short data type
o int data type
o long data type
o float data type
o double data type
Data Type Default Value Default size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte integer

int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
Floating point
double 0.0d 8 byte

The Value ranges lies between= -2n-1 to 2n-1 -1


Boolean Data Type

The Boolean data type is used to store only two


possible values: true and false. This data type is
used for simple flags that track true/false
conditions.

The Boolean data type specifies one bit of


information, but its "size" can't be defined
precisely.
Example: Boolean one = false
Byte Data Type

The byte data type is an example of primitive data type.


It is an 8-bit signed two's complement integer. Its value-
range lies between -128 to 127 (inclusive). Its minimum
value is -128 and maximum value is 127. Its default
value is 0.

The byte data type is used to save memory in large


arrays where the memory savings is most required. It
saves space because a byte is 4 times smaller than an
integer. It can also be used in place of "int" data type.

Example: byte a = 10, byte b = -20


Short Data Type

The short data type is a 16-bit signed two's complement


integer. Its value-range lies between -32,768 to 32,767
(inclusive). Its minimum value is -32,768 and maximum
value is 32,767. Its default value is 0.
The short data type can also be used to save memory
just like byte data type. A short data type is 2 times
smaller than an integer.

Example: short s = 10000, short r = -5000


Int Data Type

The int data type is a 32-bit signed two's complement


integer. Its value-range lies between - 2,147,483,648 (-
2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its
minimum value is - 2,147,483,648and maximum value is
2,147,483,647. Its default value is 0.

The int data type is generally used as a default data type


for integral values unless if there is no problem about
memory.

Example: int a = 100000, int b = -200000


Long Data Type

The long data type is a 64-bit two's complement integer.


Its value-range lies between -
9,223,372,036,854,775,808(-2^63) to
9,223,372,036,854,775,807(2^63 -1)(inclusive). Its
minimum value is - 9,223,372,036,854,775,808 and
maximum value is 9,223,372,036,854,775,807. Its
default value is 0. The long data type is used when you
need a range of values more than those provided by int.

Example: long a = 100000L, long b = -200000L


Float Data Type

The float data type takes 4 byte for storing floating


value. It is recommended to use a float (instead of
double) if you need to save memory in large arrays of
floating point numbers. Its default value is 0.0F.

Example: float f1 = 234.5f


Double Data Type

The double data type takes 8 bytes for storing the


floating point value. The double data type is generally
used for decimal values just like float. Its default value is
0.0d.

Example: double d1 = 12.3


Char Data Type

The char data type is a single 16-bit Unicode character.


Its value-range lies between '\u0000' (or 0) to '\uffff' (or
65,535 inclusive).The char data type is used to store
characters.

Example: char letterA = 'A'


Why char uses 2 byte in java and what is \u0000 ?

It is because java uses Unicode system not ASCII code


system. The \u0000 is the lowest range of Unicode
system. To get detail explanation about Unicode visit
next page.
Unicode System

Unicode is a universal international


standard character encoding that is capable
of representing most of the world's written
languages.

Why java uses Unicode System?


Before Unicode, there were many language
standards:

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, and


so on.

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 characters are 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

JAVA TOKENS

Smallest individual units in a program are known as tokens. The


compiler recognizes them for building up expressions and statements.

A java program is collection of tokens, comments and white spaces.


Java language includes five tokens:

1. Reserved ,keywords.
2. Identifiers
3. Literals
4. Operators
5. Separators
N>B: In java most of keywords and identifiers are similar to that
C/C++.However, java has maintained its uniqueness by defining
some new keywords.
1. Reserved keywords: Java language has reserved words (50) like
Boolean, break, final and so on.

2. Identifiers: These are name of the variable, name of methods,


user defined classes, interfaces and reference variable of
classes.

Java identifiers follow the following Rules:-


1. They can have alphabets, digits, underscore and dollar sign
characters.
2. They must not begin with digit.
3. Uppercase and lower letters are distinct.
4. They can be of any length.

Java developer have followed some naming conventions :->

1. Names of all methods and instance variable start with a


leading lowercase letter.
Ex: average,sum()
2. when more than one words are used in a name,the 2nd and
subsequent words are marked with leading upper letters.
Ex: dayTmperature,firstDayOfMonth,totalsMarks
3.All private and local variables use only lowere case letters
combined with underscores.
Ex:l ength, batch_strength
4.All classes and interfaces startwith a uppercase letter ( and
each subsequent word with leading uppercase letter).
Ex: Student, HelloJava,MotorCycle.
5.Variable that represent constant values user all uppercase
letters and underscores between words.
Ex:TOTAL,F_MAX,PRINCIPAL_AMOUNT

N>B:It should be remembered that all these are conventions


and not the rules .we may follow our own conventions as long
as we don’t break the basic rules of many identifiers.
3. Literals:
These are sequence of characters (digits, letters and other
characters) that represents constant values to be stored in
variables.
Java language specify following types of literals:
A. Integer literals
B. Floating point literals
C. Character literal
D. String literals
E. Boolean literals
F. Null Literals
A. Integer Literals:(Numeric literals)

These can be represented in binary, decimal, octal or hexadecimal


notations

a. Decimal integer consists of a set of digits 0 through 9 preceded


by optional minus sign.

Ex: 123, -321, 0, 654321

Embedded spaces, commas and non-digit characters


are not permitted between digits.
Ex: 15 750, 20.000, $10000// invalid

b. Octal integer constant consists of any combinations of digits


from the set 0 through 7, with a leading 0.
Ex: 037, 0, 0435, 0551
c. A sequence of digits preceded by 0x or 0X is considered as
hexadecimal integer.

They may also include alphabets ‘A through F’ or ‘a through f’

A letter ‘A’ through ‘F‘ represents the number 10


through 15.
Ex: 0x2, 0X9F, 0xbcd
d. Binary literals:

Integer types can be expressed in binary number system.

int num1=0b0110101

ob is prefixed to the value representing


binary number.(Binary number prefixed with 0b or 0B)
ex: int num b1=0B 0100101;
e. Numeric literals with underscore:
To enhance readability of large integer, allows
inserting underscore within the integer constants to
mark the place values.
int num1=1_00;
long num2=1_000_00_0001;
B. Real literals( floating literals):
Number contains fractional parts
float f=23.6F; Suffix
C. Character literals:
char A=’A’;
Single character enclose within a pair of single quote marks.
D. String literals:
String S=” This is a string literal”;
It is a sequence of characters enclosed between double quotes;
E. Boolean literals:
Boolean literal is specified as either true or false
Ex: boolean firstroll= true;
F. Null literals:
Null value is assigned to object reference variable
Ex: S=Null;
4. Operators:
An operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations (operations).
 Operator is used in programs to manipulate data and
variables.
 Operators perform actions on one or more operands.

Unary operator:( ++, --)

An operator that performs an action on one operand is called a


unary operator.

Binary operator: (+, -, /, * and more)

An operator that performs actions on two operands is called a


binary operator.

Ternary operator(? :)

An operator that performs an action on three operands is called a


ternary operator.
N.B: operator: operate on operands is a called operands
Java operators can be classified into followings:
o Arithmetic Operator
o Relational Operator
o Logical Operator
o Assignment Operator
o Increment and decrement operator
o Conditional operator
o Bitwise operator
o Special operator

1. Arithmetic Operators

Arithmetic operators are used for adding (+), subtracting (-),


multiplying (*), dividing (/) and finding the remainder (%)

Ex: a-b, a+b, a*b, a/b, a%b

It is used in integer and floating number.

% is not used in floating point number

2. Relational operator( Comparison operator):


Comparison can be done with the help of relational operator
== Equal to
!= not equal to
< Less than

> Greater than

<= less than or equal to

>= greater than or equal to

Relational operator in java return true or false as a boolean type.


3. Logical operators( short ckt operator):
These operators are used to form compound condition by
combing two or more relations.
Ex: a>b && x==10

Truth table

A B A&&B A||B !A
TRUE TRUE TRUE TRUE FALSE

TRUE FALSE FALSE TRUE FALSE

FALSE TRUE FALSE TRUE TRUE

FALSE FALSE FALSE FALSE TRUE

class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=20;
System.out.println(a<b && a<c);//false && true = false
System.out.println(a< b&& b<c);//false && true = false
}
}

Output: false
False
4. Assignment operator:

It is used to assign the value on its right to the operand on its left
Syntax:
v op= expression( short hand assignment operator or compound
assignment operator )
is equal to v=v op expression

Ex: x=x+y+1 a=a+1 a=a%b a=a*(n+1)


x+=y+1 a+=1 a%=b a*=n+1

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=20;
a+=4;
b-=4;
System.out.println(a); o/p= 14
System.out.println(b); = 16
}}

5. Increment and decrement operator

The Java unary operators require only one operand. Unary


operators are used to perform various operations i.e.:

o incrementing/decrementing a value by one


o negating an expression(~)
o inverting the value of a boolean

if x=5 what is the value of y=x++ ? first y=x=5, then


x=x+1=6(increment)

if x=5, what is the value of y=++x ? first y=x+1=5+1=6


(increment) then y=x=6(assign)
Ex: class OperatorExample
{
public static void main(String args[])
{
int x=10; output: 10
System.out.println(x++);//10 (11) 12
System.out.println(++x);//12 12
System.out.println(x--);//12 (11) 10
System.out.println(--x);//10
}}

Ex: class OperatorExample


{
public static void main(String args[])
{
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}
}
Output:

22
21

Ex;class OperatorExample
{ Output: False
public static void main(String args[]) True
{
boolean c=true;
boolean d=false;
System.out.println(!c);
System.out.println(!d);
}}
6. Condition operator( ternary operator)

Syntax: expression1? expression2: expression 3;

Ex: int a=10;


int b=15; o/p= ? 15
int x=(a>b)? a : b;

System.out.println(x);

7.Bitwise operator

Bitwise operators are used to manipulate the data at values of bit level.
These operators are used to for testing the bits or shifting them to right or
left. (Bitwise operators may not applied to float or double).

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ One’s Complement
<< Shift left
>> shift right
>>> Shift right with zero fill

N.B: && is logical AND operator comparing Boolean values of operands


& is bitwise AND operator comparing bits of each operand.

Bitwise AND Operator (&):


The bitwise AND operator performs the bitwise AND operation on each
parallel pair of bits of two operands. If the corresponding bits are one (1) in
both operands , the result is one(1) or else the result is zero.

int x=2,y=3;
A B A && B

0 0 0 System.out.println(x&y)
010
0 1 0 011
O/p=2 010
1 0 0

1 1 1
Bitwise OR(|)

The operator performs the bitwise inclusive OR operation on each


parallel pair of two operands. If any one of the operand is one in each
pair, then the result is one (1) or else the result is zero.

A B A|B
int x=2,y=3;
0 0 0
System.out.println(x|y)
0 1 1

1 0 1 O/p=3
1 1 1

Bitwise XOR operator (^)


This operator performs the exclusive OR (XOR) operation .If both operands are
same in value, the result is zero or else the result is one.

A B A^B int x=2,y=3;

0 0 0 System.out.println(x^y)
0 1 1

1 0 1 O/p=1

1 1 0
One’s Complement (~)

0 0 0 0 0 0 1 0 X

11 1 1 1 1 1 0 1 ~X

1 1’s complement
0 0 0 0 0 1 0

1
0 0 0 0 0 1 1 +1

-3

0 0 0 0 1 1 0 0 12
EX: class Demo
{
public static void main(String args[])
{int x=12; 1 1 1 1 0 0 1 1 ~12
Sytem.out.println(~x);
}} 1 0 0 0 1 1 0 0 1’s complement

1 0 0 0 1 1 0 1 2’s complement

-13

EX: class Demo


{
public static void main(String args[])
0 0 0 0 1 1 0 0 12
{int x=-12;
Sytem.out.println(~x);
}} 1 1 1 1 0 0 1 1 1’s complement

1 1 1 1 01 0 0 2’s complement

11 0 0 0 0 1 0 1 1 ~ (-12)=12
Ex-3
EX2: class Demo Class demo
{ {public static void main(String srgs[])
public static void main(String args[]) Int x=12
{int x=-12; Int y=13 o/p=13
Int y=13 o/p=4 x=x|y;
x=x&y; Sytem.out.println(x);
Sytem.out.println(x);
}}

Ex-4 Ex-3
Class demo Class demo
{public static void main(String srgs[]) {public static void main(String srgs[])
Int x=12 Int x=-12
Int y=13 o/p=1 Int y=13 o/p=-7
x=x^y; x=x ^y;
Sytem.out.println(x); Sytem.out.println(x);

Bitwise shift operators:

It is used to manipulate the contents of variable at a bit level according


to binary format

a. Bitwise right shift operator(>>)


b. Bitwise left shift operator(<<)

Bitwise right shift operator(>>)


The signed right shift (>>) operator shifts a bit (or bits) to the right by the
distance specified in the right operand and fills the left most bit by the sign
bit.
The right most bit (or bits) is shifted out and if the left operand is +ve and
new ‘0’ is filled with sign bit into high order bits to left position , otherwise
,if the left operands is -ve , it is filled up with one(1).

Syntax:
Variable >>no. of bits shift
Ex: X>>2
Bit wise right shift operator shift the bits present in the variable to a
specified no.of bits towards right .When a positive number is right shifted ,
it is filled by ‘0’ and for negative number it is filled by ‘1’.
-5>>2
Ex: 5>>2
0 0 0 0 0 1 0 1
5 0 0 0 0 0 1 0 1

5>>2 1 1 1 1 1 0 1 0

1 0 0 0 0 0 0 0 1
2’s complement 1 1 1 1 1 0 1 1

>>2

After shifting 1 1 1 1 1 1 1 0

1 0 0 0 0 0 0 1

1 0 0 0 0 0 1 0 -2

Bitwise –left shift operator(<<)


The signed left shift (<<) operator shifts a bit (or bits) to the left by
the distance specified in the right operand. In this case, the
leftmost digit is shifted at the end of the register and a new ‘0’ is
shifted into the rightmost position.
In a left shift operation, the bits are filled with zero irrespective of
their polarity of the operand, whether it is negative or positive.
Syntax: variable << no.of shifts
Ex: 5 << 2
0 0 0 0 0 1 0 1 << 2 0 0 0 1 0 1 0 0 20
1 1 1 1 1 0 1 1
-5
Ex: -5 << 2
<<2

1 1 1 0 1 1 0 0

1 0 0 1 0 0 1 1 1’s complement

1 0 0 1 0 1 0 0 2’s complement

-20

Bitwise unsigned right shift operator(>>>)


Bitwise unsigned right shift operator shifts the bits of a number to a specified
number of bits towards right without preserving the sign bit. Hence we can say
that empty bits are filled with ‘0’.

So, the result of applying the (>>>) operator is always positive.

An unsigned shift operation of a negative number generally returns the result in a


positive number because any unsigned right shift operation replaces the leading
sign bit with a zero which indicates a positive number.

Syntax: variable >>> no.of bits shift

Ex: 64 >>>4 -64 >>>4

64 -64
0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0

64>>>4 >>>4

0 0 0 0 0 1 0 0 4 0 0 0 0 1 1 0 0 ?
Special operator
1. Instanceof operator
2. Member selection operator
1. This operator allows us to determine whether the object belong to
particular class or not.
Ex: person instance of student is true if the object person belongs to the
class student ,otherwise it is false.

Syntax: object instanceof <class type>

Ex:class Demo
{
public static void main(String args[])
{
String s =”Hello”;
If ( s instanceof java.lang.String) o/p= it is a string
{
Sytem.out.println(“It is a string”);
}}}
2.dot operator (.)

It is used to access the instance variable and methods of class objects.


Ex: person1.age//reference to the variable age
person1.salary()//reference to the method salary()
It is also used to access classes and sub packages from package
Separator:
Separators are symbols used to indicate whether groups of code are divided or
arranged.
() parenthesis: used in method definition and invocation
{} braces : used to define a block of codes for classes or method
[] brackets: used to declare array size .
; semicolon: used to separate statements or end of statements.
, comma: separate consecutive identifier in a variable declaration.
. (period): used to separate a variable or method from a reference variable or
used to separate package names from sub packages from classes.

You might also like