[go: up one dir, main page]

0% found this document useful (0 votes)
13 views22 pages

Datatyped and Operators

Uploaded by

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

Datatyped and Operators

Uploaded by

srividya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

OOP

Java Versions
Simple Program
Comments
Identifiers
Data types
Variables
Operators
JAVA Versions Till now
• JDK Alpha and Beta (1995) • Java SE 8 (18th March, 2014)
• JDK 1.0 (23rd Jan, 1996) • Java SE 9(September 2017)
• JDK 1.1 (19th Feb, 1997) • Java SE 10 (March 2018)
• J2SE 1.2 (8th Dec, 1998) • Java SE 11 (September 2018)
• J2SE 1.3 (8th May, 2000) • Java SE 12 (March 2019)
• J2SE 1.4 (6th Feb, 2002) • Java SE 13 ( September 2019)
• J2SE 5.0 (30th Sep, 2004) • Java SE 14 ( March 2020)
• Java SE 6 (11th Dec, 2006) • Java SE 15 ( September 2020)
• Java SE 7 (28th July, 2011) • Java SE 16 (March 2021)
A First Simple Program

To Compile:
To Run:
Comments
Comments can be used to explain Java code, and
to make it more readable.
1. Single-line comments start with two forward
slashes (//).
2. Multi-line comments start with /* and ends
with */
3. Documentation Comment: To create Java
API(Application Programming Interface)
document. Start with /** end with */
Identifiers
Identifiers are used to name things, such as classes, variables,
and methods. An identifier may be any descriptive sequence
of uppercase and lowercase letters, numbers, or the
underscore.
• Java is case-sensitive, so VALUE is a different identifier than
Value. Some examples of valid identifiers are

• Invalid identifier names include these:


Data types
Data types represent the different values to be
stored in the variable.
Integer Data types
Data Type Memory size Range

Byte 1 byte -128 to 127

Short 2 bytes -32768 to 32767

Int 4 bytes -2147483648 to 2147483647

Long 8 bytes -9223372036854775808 to 9223372036854775807

Floating Data types


Data Type Memory size Range
Float 4 bytes -34e38 to 34e38
Double 8 bytes -17e308 to 17e308

Character Data types


Data Type Memory size Range
Char 2 bytes 0 to 65535

Boolean Data Type: can handle truth values either


true or false
Default Values

Difference between float and Double:


float can give you 7 digits decimal points precision. double is a
double precision floating point operation. In other words, double
can give you 15 decimal points precision.
Variables
• What is a variable?
• The name of some location of memory used to hold a data value
• Different types of data require different amounts of memory. The
compiler’s job is to reserve sufficient memory
• Variables need to be declared once
• Variables are assigned values, and these values may be changed later
• Each variable has a type, and operations can only be performed
between compatible types
Data types and Variables
• Java  Strongly-type language
• Strong Type Checking  Java checks that all expressions involve compatible types
• int x, y; // x and y are integer variables
• double d; // d is a double variable
• String s;// s is a string variable
• boolean b; // b is a boolean variable
• char c; // c is a character variable

• x = 7; // legal (assigns the value 7 to x)


• b = true; // legal (assigns the value true to b)
• c = ‘#’; // legal (assigns character # to c)
• s = “cat” + “bert”; // legal (assigns the value “catbert” to s)
• d = x – 3; // legal (assigns the integer value 7 – 3 = 4 to double d)

• b = 5; // illegal! (cannot assign int to boolean)


• y = x + b; // illegal! (cannot add int and boolean)
• c = x; // illegal! (cannot assign int to char)
Types of Variable
There are three types of variables in java:
1. Local Variable
A variable which is declared inside the method is
called local variable.
2. Instance Variable
A variable which is declared inside the class but
outside the method, is called instance variable . It
is not declared as static.
3. Static variable
A variable that is declared as static is called static
variable. It cannot be local.
Example to understand the types of variables in java

class A{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class
Scope and Life Time of Variables
• The scope of a variable defines the section of
the code in which the variable is visible.
• The lifetime of a variable refers to how long the
variable exists before it is destroyed.
Instance variables
The scope of instance variables is determined by
the access specifier that is applied to these variable.
The lifetime of these variables is the same as the
lifetime of the object to which it belongs
Argument variables
The scope of these variables is the method or
constructor in which they are defined.
The lifetime is limited to the time for which the
method keeps executing. Once the method finishes
execution, these variables are destroyed.
Local variables
The scope and lifetime are limited to the method itself.
Operators
An operator is a symbol that performs an
operation. An operator acts on variables called
operands.
a+b
here a, b are Operands
+ is Operator
Arithmetic operators: These operators are used to perform
fundamental operations like addition, subtraction, multiplication
etc
Operator Meaning Example Result

+ Addition 3+4 7

- Subtraction 5-7 -2

* Multiplication 5*5 25

/ Division (gives quotient) 14 / 7 2

% Modulus (gives remainder) 20 % 7 6

Assignment operator: This operator (=) is used to store some value


into a variable
Simple Assignment Compound Assignment
x=x+y x += y
x=x–y x -= y
x=x*y x *= y
x=x/y x /= y
Unary operators: As the name indicates unary
operator‟s act only on one operand

Operator Meaning Example Explanation

- Unary j = -k; k value is negated and stored into j


minus
b value will be incremented by 1
Increment b++; (called as post incrementation)
++ Operator ++b; b value will be incremented by 1
(called as pre incrementation)
b value will be decremented by 1
Decrement b--; (called as post decrementation)
-- Operator --b; b value will be decremented by 1
(called as pre decrementation)
Relational operators: These operators are used
for comparison purpose
Operator Meaning Example
== Equal x == 3

!= Not equal x != 3

< Less than x<3

> Greater than x>3

<= Less than or equal to x <= 3


Logical operators: Logical operators are used
to construct compound conditions A
compound condition is a combination of
several simple conditions
Operator Meaning Example Explanation

if(a>b && a>c) If a value is greater than b and c


&& and operator
System.out.print(“yes”); then only yes is displayed

If either a value is 1 or b value is


if(a==1 || b==1)
1
|| or operator
System.out.print(“yes”); then yes is displayed
If a value is not equal to zero
if( !(a==0) )
then
! not operator
System.out.print(“yes”); only yes is displayed
Bitwise operators: These operators act on
individual bits (0 and 1) of the operands They
act only on integer data types, ie byte, short,
long and int
Operator Meaning Explanation

& Bitwise AND Multiplies the individual bits of operands


| Bitwise OR Adds the individual bits of operands

^ Bitwise XOR Performs Exclusive OR operation

Shifts the bits of the number towards left a specified


<< Left shift
number of positions
Shifts the bits of the number towards right a

>> Right shift specified number of positions and also preserves the

sign bit
Shifts the bits of the number towards right a
>>> Zero fill right shift specified number of positions and it stores 0 (Zero)
in the sign bit
Gives the complement form of a given number by
~ Bitwise complement
changing 0‟s as 1‟s and vice versa
Ternary Operator or Conditional Operator (? :)
This operator is called ternary because it acts on
3 variables The syntax for this operator is:
• Variable = Expression1? Expression2: Expression3;
• First Expression1 is evaluated If it is true, then
Expression2 value is stored into variable
otherwise Expression3 value is stored into the
variable
• eg: max = (a>b) ? a: b;

You might also like