0% found this document useful (0 votes)
28 views40 pages

Understanding Java Variables and Types

The document provides an overview of Java variables, including their types, data types, and memory allocation. It details primitive data types such as byte, short, int, long, float, double, char, and boolean, along with their characteristics and examples. Additionally, it covers variable declaration, constants, literals, and various operators used in Java programming.

Uploaded by

Ankit kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views40 pages

Understanding Java Variables and Types

The document provides an overview of Java variables, including their types, data types, and memory allocation. It details primitive data types such as byte, short, int, long, float, double, char, and boolean, along with their characteristics and examples. Additionally, it covers variable declaration, constants, literals, and various operators used in Java programming.

Uploaded by

Ankit kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Java Variables

Variables are nothing but reserved


memory locations to store values. This
means that when you create a variable
you reserve some space in memory
Based on the data type of a variable,
the operating system allocates memory
and decides what can be stored in the
reserved memory.
Data type

Non-Primitive

Primitive Data
Types
Referred as derived
data type
There are eight
primitive data types
supported by Java.
Primitive data types
are predefined by the classes Array
Interface
language and named
by a key word.
Primitive Data Types:

Non
Numeric
Numeric

characteBoolea
integer floating point
r n
float doubl
shor int e
byte long
t
► byte:
► Byte data type is a 8-bit signed two.s
complement integer.
► Minimum value is -128 (-2^7)
► Maximum value is 127 (inclusive)(2^7 -1)
► Default value is 0
► Byte data type is used to save space in large
arrays, mainly in place of integers, since a
byte is four times smaller than an int.

► Example : byte a = 100 , byte b = -50


short:

► Short data type is a 16-bit signed two's


complement integer.
► Minimum value is -32,768 (-2^15)
► Maximum value is 32,767(inclusive) (2^15 -1)
► Short data type can also be used to save
memory as byte data type. A short is 2 times
smaller than an int
► Default value is 0.
► Example : short s= 10000 , short r = -20000
int
► Int data type is a 32-bit signed two's
complement integer.
► Minimum value is - 2,147,483,648.(-2^31)
► Maximum value is 2,147,483,647(inclusive).
(2^31 -1)
► Int is generally used as the default data type
for integral values unless there is a concern
about memory.
► The default value is 0.
► Example : int a = 100000, int b = -200000
long:

► Long data type is a 64-bit signed two's


complement integer.
► Minimum value is -
9,223,372,036,854,775,808.(-2^63)
► Maximum value is
9,223,372,036,854,775,807 (inclusive).
(2^63 -1)
► This type is used when a wider range than int
is needed.
► Default value is 0L.
► Example : int a = 100000L, int b = -200000L
floating point
float:
► Float data type is a single-precision
32-bit floating point.
► Float is mainly used to save memory in
large arrays of floating point numbers.
► Default value is 0.0f.
► Example : float f1 = 234.5f or 234.5F
double:

► double data type is a double-precision


64-bit floating point.
► This data type is generally used as the
default data type for decimal values.
generally the default choice.
► Default value is 0.0d.
► Example : double d1 = 123.4
char:
 Used to store character constant in memory
that is a letter,a digit,a punctuation mark,a
tab,a space or something similar
 e.g char ch=“:”;char p=‘A’;
 In ASCII character set ranges from 0 to 127
and needs 8 bit to represent a character
 Since Java uses the UNICODE character
[Link] defines a fully international
character set found in all human languages
and writing system such as
English,Arabic,Chinese etc.
Due to large number of
languages a large language set
is required

And 8 bit is not


[Link] it needs
16 bit character set .
It ranges from ‘\u000’(or 0)
to ‘\uffff’(or 65,535 )
boolean

► boolean data type represents one bit of


information.
► There are logical values and contains only two
possible values : true and false.
► This data type is used for simple flags that track
true/false conditions.
► Default value is false.
► Example : boolean one = true
► All comparison operator return boolean type value
few special escape sequences
for String and char literals
[Link]
Programs wd different variable
types
► [Link]
Array
► It is a group of liked-typed variable
that are referred to by a common
name.
► Specific element in an array is
accessed by its index
► Its of two type:
► One dimensional array
► Multi dimensional array
Arrays

Multi-dimensional arrays
One dimensional
arrays

[Link] Irregular multi


[Link] 4d array
dimensional
e.G: array
double ac[][][]
Eg:double arr[]
[]=new double[2][3]
[]=new double[5][];
[4][5]
Arr[0]=new
[Link](3d double[500];
array)
Arr[1]=new
double[400];

arr_vsize.jav
a
A java program to print the output
arr_vsize.java

output

1
14
149
1 4 9 16
Student’s task
[Link] the area of circle.
[Link] a program in java to implement the
formula Area=height*Width i.e. area of a
rectangle.
[Link] a program in java to compute the sum
of a given integers.
[Link] a program in java to find A+B where A
is a matrix of 3*3 and B is 3*3
[Link] a program in java to find A*B where A
is a matrix of 3*3 and B is a matrix of 3*4
[Link] a program in java to find the average
of marks you obtained in 5 papers.
Variable
► Are the names of storage location.
► Variable declaration means
► i)It inform the compiler the variable
name
► Ii)specify the type of data the variable
will be using
► Iii)The place of declaration describes
the scope of the variable
► Variable are given values
► i)By using an assignment statement i.e
int x;x=100;
► By using read statement.
► E.g.:[Link]
Kinds of java variables
on the basis of scope

Instance variable Class variable

Local
It is similar to
Used to define the variable
instance
attributes of a
variable,except their
particular object
values apply to all
the instance of a
They are declared
class(and to the
They take and used inside
class itself)rather
different value methods,for
than
Classhaving
variable are
for each object eg,counter in loops
different
global to values
a class for
and
each
belong object
to the entire
set of objectsl that
class
Only creates
one memory
location is created
for each class
Constants(fixed values that do
not change during execution of
a program)

Numeric constant Character


constant
character String
real
integer
‘5’,’X’ “well
0.0083 done”
037,04
5,0x2
literals
► Are constant value that is used.
For e.g. int days=365
Types of literals

Numb Boolean Characte String Double


er literals r literals literals literals
literals
- True,false ‘a’,’#’,’ “hello” 1.5,45.6
45,4l,077 Long 3’ Float
7 literals literals

45.6f
34l
Java operators

Arithmet Assignmen Multiple RelationaBoolean


Bitwise
ic t operators Assignme l Operator operat
operator nt operator
or
s

Class and
object operator

Other
operators
Arithmetic ► Binary Operators

operator
operator Use Description
+ A+B Adds A and B

- A-B Subtracts B from A

* A*B Multiplies A by B

/ A/B Divides A by B

% A%B Computes the


remainder of dividing
A by B

High priority * / % 1) Left to right pass;

Low priority + - 2)Process high priority;


3)Process low priority;
Unary operator
Operator Use Description
++”post increment” A++ The value is assigned
before the increment is
made,e.g.
A=1;
B=A++;Then B will hold 1
and A will hold 2.
--”Post decrement” A-- The value is assigned
before the decrement is
made.
e.g:A=1;B=A--;
Then B will hold 1 and A will
hold 0.
++”Pre increment” ++A Pre-increment:The value is
assigned after the
increment is made.e.g.
A=1;B=++A;Then B will
hold 2 and A will hold 2.
--A The value is assigned after
--”Pre-decrement” the decrement is made,e.g:
A=1;B=--A;
Assignment operators
Operator Use Equivalent to

+= A+=B A=A+B
-= A-=B A=A-B
*= A*=B A=A*B
/= A/=B A=A/B
%= A%=B A=A%B
&= A&=B A=A&B

|= A|=B A=A|B
^= A^=B A=A^B
<<= A<<=B A=A<<B
>>= A>>=B A=A>>B

>>>= A>>>=B A=A>>>B


MULTIPLE ASSIGNMENT
► A=B=786+x+y+a
i.e.
A=786+x+y+a
B=786+x+y+a
Relational operator(comparison
operator)
Operator Use Returns true if
> A>B A is greater than B

>= A>=B A is greater than or


equal to B
< A<B A is less than B

<= A<=B A is less than or equal


to B
== A==B A and B are equal

!= A!=B A and B are not equal


Boolean operator
Operator Use description
&& A&&B Conditional And:if both are true,result is [Link]
either A or B are false,the result are false
|| A||B Conditional Or:if either A or B are true,the result
is true
! !A Boolean not:if a is true,result is false
& A&B Boolean AND:if both A and B are true,the result
is [Link] either A or B are false,the result is false

| A|B Boolean OR:If either A or B are true,the result is


[Link] A and B are evaluated befor the test
^ A^B Boolean XOR:If A is true and B is false,the result
is [Link] A is false and B is true ,the result is
[Link],the result is [Link] A and B
are evaluated before the test.
Bitwise operator
Operator Use Description
>> A>>B Right Shift:It shift bits of
A=60i.e00111100 A right by distance B,0
is introduced to the
B=2
vacated most
significant bits)and the
vacated least
significant bits are lost.
For e.g:A>>B will give
1111 as output which is
15

<< A<<B Left shift:Shift bits of A


left by distance B bitsi.e
the most significant bits
are lost and the
vacated least
significant bits are zero
For e.g A<<B will give
11110000 as output
Cont..
operator Use Description
>>> A>>>B Right shift
unsigned:shift A to the
right by B [Link]
order bits are [Link]
fill in the left bits
regardless of sign
example
i.E A>>>B
means00001111 i.e 15
~ (~A) Bitwise Complement:
It changes 1 into 0 and
0 into 1
Hence(~A) will give
11000011 as A is
00111100
Cont..
Operator Use description
& A&C Bitwise AND:AND is 1 if
A 60 00111100 both the bits are 1
otherwise AND is 0
C 13 00001101
The outpot of A & C is
00001100 ,decimal value
is 12
| A|C Bitwise OR:OR is 1 if either
the bits is 1,otherwisee it
is 0
The output of A|C is
00111101 decimal value is
61
^ A^C Bitwise Exclusive OR:it is
true if either bits are true
but not
both,otherwise it is 0
Hence the output is
00110001 decimaal value
Class and object operator
Operator Name Description
Instanceof Object instanceof class The instanceof is an
object reference operator
For e.g
and returns true if the
Dog instanceof animal object on the left-hand
Returns true if dog is side is an instance of the
object of class animal class given on the right-
hand side
New Class instantiation Creates a new [Link]
e.g:new A,in this A is
either a call to
constructor ,or an array
specification
“.” Class Member Access It access a method or
field of a class or
[Link] e.g:
[Link],[Link]()
() Method invocation Declares or call the
method name A with
specified parameterfor
e.g:A(parameter)
(Type) Object cast Convert A to specific
[Link] e.g (type)A
CASTING OPERATOR
Casting is used to explicitly convert the value of one type to
another.

The result of cast is a new reference or a value.

In Java ,not only data types but objects are also created.

This casting is of two types:


i)Casting of Primitive data types
ii)Casting of Object type.

Casting primitive data types


Explicitly convert one primitive data type into another.
In java , boolean data type cannot be casted to any other data
type.
If one wants to cast a smaller data type to a larger one then implicitly
casting is done in java.
The larger char
type provides more precision than the smaller type and
hence no information is lost.
int long float doubl
shor
•When the larger type is assigned to the
smaller type then explicit cast must be
required.

Explicit cast is require when the flow of


data moves from the opposite direction

[Link]
OTHER OPERATORS

Operator Use Description


?: A?B:C If A is true,returns
[Link] return C
[] Type[] Declares an array of
unknown length,which
Contains type elements
[] Type[A] Creates an array with A
[Link] be used
with new operator
[] A[B] Access the element at
index B within the array
[Link] begin at 0 and
extend through the
length of the array minus
1.
+ A+B Binary operator which
concatenates one string
to [Link] e.g:string
str1=“stud’;string
str2=“ent”
String str3=str1 + str2
Operator precedence
() [] .
++ -- ~ !
* / %
+ -
>> >>> <<
> >= < <=
== !=
&
^
|
&&
||
?:
= [operator]=
Student’s task
► Q1) Write a program in java to find the result of the following
expression(assume a=10,b=5)
► i)(a<<2) + (b>>2)
► ii)(a)||(b>0)
► iii)(a+b*100)/10
► iv)a & b
► Q2)Difference between boolean(&&) and bitwise(&) operator
► Q3)write a java program to have screen display
1
22
333
4444
55555
Q4)5 10 15 20 25
4 8 12 16
3 6 9
2 4
1

You might also like