JAVA BASICS

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

INTRODUCTION TO JAVA

KIT-501
VARIABLE IN JAVA
Variable in Java is a data container that stores the data values during Java
program execution. Every variable is assigned data type which designates
the type and quantity of value it can hold. Variable is a memory location
name of the data. The Java variables have mainly three types : Local,
Instance and Static.

In order to use a variable in a program you to need to perform 2 steps


1. Variable Declaration

2. Variable Initialization
TYPES OF VARIABLES
In Java, there are three types of variables:

1) Local Variables
• Local Variables are a variable that are declared inside the body of a method.

2) Instance Variables
• Instance variables are defined without the STATIC keyword .They are defined Outside a
method declaration. They are Object specific and are known as instance variables.

3) Static Variables
• Static variables are initialized only once, at the start of the program execution. These
variables should be initialized first, before the initialization of any instance variables.
DATA TYPES IN JAVA
Data Types in Java are defined as specifiers that allocate different sizes
and types of values that can be stored in the variable or an identifier.
Java has a rich set of data types. Data types in Java can be divided into
two parts :
• Primitive Data Types :- which include integer, character, boolean, and float

• Non-primitive Data Types :- which include classes, arrays and interfaces.


HELLO WORLD EXAMPLE

class A {

public static void main(String args[])

System.out.println("Hello World");

}
PRIMITIVE DATA TYPES
• Primitive Data Types are predefined and available within the Java language.
Primitive values do not share state with other primitive values.
• There are 8 primitive types: byte, short, int, long, char, float, double, and Boolean

DATA TYPE DEFAULT VALUE DEFAULT SIZE


byte 0 1 byte
short 0 2 bytes
int 0 4 bytes
long 0L 8 bytes
float 0.0f 4 bytes
double 0.0d 8 bytes
boolean false 1 bit
char ‘\u0000’ 2 bytes
VARIABLE TYPE CONVERSION &
TYPE CASTING
A variable of one type can receive the value of another type. Here there are
2 cases –
Case 1) Variable of smaller capacity is be assigned to another variable of
bigger capacity.
This process is Automatic, and non-explicit is known as Conversion
Case 2) Variable of larger capacity is be assigned to another variable of
smaller capacity.
In such cases, you have to explicitly specify the type cast operator. This
process is known as Type Casting.
In case, you do not specify a type cast operator; the compiler gives an error.
EXAMPLE
class Demo {
public static void main(String args[]) {
byte x;
int a = 270;
double b = 128.128;
System.out.println("int converted to byte");
x = (byte) a;
System.out.println("a and x " + a + " " + x);
System.out.println("double converted to int");
a = (int) b;
System.out.println("b and a " + b + " " + a);
System.out.println("\ndouble converted to byte");
x = (byte)b;
System.out.println("b and x " + b + " " + x);
}
PRINT IN JAVA
Three methods or functions are provided in the Java language to print
the output.
1.Java print() method: Data displays in the same line using print
statement in java.
2.Java println()Method: Output print in the current line, and cursor
position moves to the next line.
3.Java printf()Method: data displays with the particular format as
per requirement.
class <class_name>{ field; method; }

CLASS AND OBJECT


Class are a blueprint or a set of instructions to build a specific type of object. It is a
basic concept of Object-Oriented Programming which revolve around the real-life
entities. Class in Java determines how an object will behave and what the object
will contain.
class <class_name>
{
field; method;
}
• Object is an instance of a class. An object in OOPS is nothing but a self-contained
component which consists of methods and properties to make a particular type
of data useful. From a programming point of view, an object in OOPS can include
a data structure, a variable, or a function.
Operators in Java
• Operator in Java is a symbol that is used to perform
operations. For example: +, -, *, / etc.
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
UNARY OPERATOR
Unary operator requires only a single operand; this operator is used to
increment or decrement the value, negating an expression or inverting
a Boolean value.
OPERATOR DESCRIPTION

This operator is used to increment the value


++ (Increment) by 1. There are two types of increment, i.e.,
post-increment (a++) and pre-increment (++a)

This operator is used to decrement the value


— (Decrement). by 1. There are two types of decrement, i.e.,
post decrement (a–) and pre decrement (–a)

This operator is used to invert a boolean value


! (Invert)
(!a).
ARITHMETIC OPERATOR
Arithmetic operators are used to performing addition, subtraction, multiplication,
division, and modulus. It acts as a mathematical operations.

OPERATOR DESCRIPTION
+ ( Addition ) This operator is used to add the value of the operands.

This operator is used to subtract the right-hand operator with the


– ( Subtraction )
left hand operator.

* ( Multiplication ) This operator is used to multiply the value of the operands.

This operator is used to divide the left hand operator with right
/ ( Division )
hand operator.

This operator is used to divide the left hand operator with right
% ( Modulus )
hand operator and returns remainder.
ASSIGNMENT OPERATOR
Assignment operator are used to assign new value to a variable. The left side
operand of the assignment operator is called variable and the right side operand of
the assignment operator is called value.

OPERATOR DESCRIPTION
= This operator is used to assign the value on the right to the operand on the left.

+= This operator is used to add right operand to the left operand and assigns the result to the left operand.

-= This operator subtracts right operand from the left operand and assigns the result to the left operand.

*= This operator multiplies right operand with the left operand and assigns the result to the left operand.

/= This operator divides left operand with the right operand and assigns the result to the left operand.

^= This operator performs exponential calculation on operators and assigns value to the left operand
This operator is used to divide the left-hand operator with right hand operator and assigns the result to left
%=
operand.
LOGICAL OPERATOR
Logical operators are used to combining two or more conditions or complement
the evaluation of the original condition under consideration.

OPERATOR DESCRIPTION

This operator returns True if both the operands are


&& (Logical AND)
true, otherwise, it returns False.

This operator returns True if either the operands are


|| (Logical OR)
true, otherwise it returns False.
SHIFT OPERATOR
The shift operator is used to shift the bits of a number left or right by
multiplying or dividing the numbers.

OPERATOR DESCRIPTION

This operator left shifts the bits of the first operand; the second operand
<< (Left Shift)
decides the number of places to shift.

This operator right shifts the bits of the first operand; the second operand
>> (Right Shift)
decides the number of places to shift.
RELATIONAL OPERATOR
Relational operator compares two numbers and returns a boolean value. This operator is
used to define a relation or test between two operands.

OPERATOR DESCRIPTION
This operator returns True, if the value of the left operand is less than the value of the
< (Less than)
right operand, else it returns False.
This operator returns True, if the value of the left operand is greater than the value of the
> (Greater than)
right operand, else it returns False.

This operator returns True, if the value of the left operand is less than or equal to the
<= (Less than or equal to)
value of the right operand, else it returns False.

>= (Greater than or equal This operator returns True, if the value of the left operand is greater than or equal to the
to) value of the right operand, else it returns False.
== (Equal to) This operator returns True, if two operands are equal, else it returns False.
!= (Not equal to) This operator returns True, if two operands are not equal, else it returns False.
JAVA OPERATOR PRECEDENCE AND ASSOCIATIVITY
• OPERATOR PRECEDENCE : Operator precedence determines which operator is performed first in
an expression if there is more than one operators with different precedence.
• OPERATOR ASSOCIATIVITY : Operator associativity is used when an expression have two
operators having same precedence.
OPERATOR PRECEDENCE ASSOCIATIVITY
Unary ++expr —expr +expr –expr ~ ! right to left
Multiplicative */% left to right
Additive +– left to right
Shift << >> >>> left to right
Relational < > <= >= instanceof left to right
Equality == != left to right
bitwise AND & left to right
bitwise exclusive OR ^ left to right
bitwise inclusive OR | left to right
logical AND && left to right
logical OR || left to right
Ternary ?: right to left

Assignment = += -= *= /= %= &= ^= |= <<= >>= >>>= right to left

You might also like