Vous êtes sur la page 1sur 24

Unit - 1

Introduction
Prof. Arjun Bala OOP JAVA
9624822202 2150704
arjun.bala@darshan.ac.in Semester 5
Computer
Computer Engineering
Engineering Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Features of JAVA
 Simple  Architecture-neutral
 Secure  Interpreted
 Portable  High Performance
 Object-oriented  Distributed
 Robust  Dynamic
 Multithreaded

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Hello World Java Program File must be saved as
HelloWorld.java

public class HelloWorld Main method from where


execution will start
{
public static void main(String args[])
{
System.out.println(“Hello World”);
String must start with capital letter

}
}
System must start
with capital letter

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
5

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Primitive Data Types
 Java defines 8 primitive types
• byte
• short
• int
• long
• char
• float
• double
• boolean

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
byte
 Smallest integer type
 It is a signed 8-bit type (1 Byte)
 Range is -128 to 127
 Especially useful when working with stream of data from a
network or file
 Example:
byte b = 10;

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
short
 short is signed 16-bit (2 Byte) type
 Range : -32768 to 32767
 It is probably least used Java type
 Example:
short vId = 1234;

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
int
 The most commonly used type
 It is signed 32-bit (4 Byte) type
 Range: -2,147,483,648 to 2,147,483,647
 Example:
int a = 1234;

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
long
 long is signed 64-bit (8 Byte) type
 It is useful when int type is not large enough to hold the desired
value
 Example:
long soconds = 1234124231;

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
char
 It is 16-bit (2 Byte) type
 Not like c/c++ (in c/c++ it is only 1Byte)
 Because java is uses Unicode to represent all the character set
 Range: 0 to 65,536
 Example:
char first = ‘A’;
char second = 65;

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
float
 It is 32-bit (4-Byte) type
 It specifies a single-precision value
 Example:
float price = 1234.45213f; output = 1123.4521
float price = 12345.45213f; output = 12345.452

Have to write f at
the end

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
double
 It uses 64-bit (8-Byte)
 All math functions such as sin(),cos(),sqrt() etc… returns double
value
 Example:
double pi = 3.14141414141414;

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
String Literals
 Character Escape Sequences

Escape Sequence Description


\’ Single quote
\” Double quote
\\ Backslash
\r Carriage return
\n New Line
\t Tab

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Automatic Type Casting
 When one type of data is assigned to other type of variable , an
automatic type conversion will take place if the following two
conditions are meet:
• The two types are compatible
• The destination type is larger than the source type
 Such type is called “widening conversion”.
 Example:
Int can always hold values of byte and short

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Casting Incompatible Types
 To create a conversion between two incompatible types, you must
use a cast
 A cast is an explicit type conversion.
 Such type is called “narrowing conversion”.
 Syntax:
(target-type) value
 Example:
int a = 5;
short b = (short) a;

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Operators
i. Arithmetic Operators
ii. Relational Operators
iii. Bitwise Operators
iv. Logical Operators
v. Assignment Operators
vi. Misc Operators

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
i. Arithmetic Operator
Operator Description Example
+ Addition - Adds values on either side of the A + B will give 30
operator
- Subtraction - Subtracts right hand operand A - B will give -10
from left hand operand
Example,
* A = 10values on either
Multiplication - Multiplies A * B will give 200
side of the operator B = 20

/ Division - Divides left hand operand by right B / A will give 2


hand operand
% Modulus - Divides left hand operand by right B % A will give 0
hand operand and returns remainder

++ Increment - Increases the value of operand by B++ gives 21


1
-- Decrement - Decreases the value of operand B-- gives 19
by 1

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
ii. Relational Operators
Operator Description Example
== Checks if the values of two operands are equal or not, if (A == B) is not
yes then condition becomes true. true.
!= Checks if the values of two operands are equal or not, if (A != B) is true.
values are not equal then condition becomes true.
> Checks if the value of left operand is greater than the (A > B) is not
value of right operand, if yes then condition becomes true.
true.
< Checks if the value of left operand is less than the value (A < B) is true.
of right operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or (A >= B) is not
equal to the value of right operand, if yes then true.
condition becomes true.
<= Checks if the value of left operand is less than or equal (A <= B) is true.
to the value of right operand, if yes then condition
becomes true.

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
iii. Bitwise Operators
Operator Description Example
& Binary AND Operator copies a bit to the result if (A & B) will give 12 which is
it exists in both operands. 0000 1100
| Binary OR Operator copies a bit if it exists in (A | B) will give 61 which is
either operand. 0011 1101
^ Binary XOR Operator copies the bit if it is set in (A ^ B) will give 49 which is
one operand but not both. 0011 0001
~ Binary Ones Complement Operator is unary and (~A ) will give -61 which is 1100
has the effect of 'flipping' bits. 0011 in 2's complement form
Example, due to a signed binary number.

<< A = 60 The left operands


Binary Left Shift Operator. A << 2 will give 240 which is
value is moved leftBby = 13
the number of bits 1111 0000
specified by the right operand.
>> Binary Right Shift Operator. The left operands A >> 2 will give 15 which is
value is moved right by the number of bits 1111
specified by the right operand.
>>> Shift right zero fill operator. The left operands A >>>2 will give 15 which is
value is moved right by the number of bits 0000 1111
specified by the right operand and shifted
values are filled up with zeros.

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
iv. Logical Operators
Operator Description Example
&& Called Logical AND operator. If both (A && B) is
the operands are non-zero, then the false.
condition becomes true.

|| Called Logical OR Operator. If any of (A || B) is


the two operands
Example,are non-zero, then true.
the condition becomes true.
A = true
B = false
! Called Logical NOT Operator. Use to !(A && B) is
reverses the logical state of its true.
operand. If a condition is true then
Logical NOT operator will make false.

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
v. Assignment Operators
Operator Description Example
= Simple assignment operator, Assigns values from right side C = A + B will assign value of A
operands to left side operand + B into C
+= Add AND assignment operator, It adds right operand to the left C += A is equivalent to C = C +
operand and assign the result to left operand A
-= Subtract AND assignment operator, It subtracts right operand C -= A is equivalent to C = C - A
from the left operand and assign the result to left operand
*= Multiply AND assignment operator, It multiplies right operand C *= A is equivalent to C = C *
with the left operand and assign the result to left operand A
/= Divide AND assignment operator, It divides left operand with C /= A is equivalent to C = C / A
the right operand and assign the result to left operand
%= Modulus AND assignment operator, It takes modulus using C %= A is equivalent to
two operands and assign the result to left operand C=C%A
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2

&= Bitwise AND assignment operator C &= 2 is same as C = C & 2


^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
vi. Misc Operators
 Conditional Operator ( ? : )
• Syntax:
variable x = (expression) ? value if true : value if false
• Example:
b = (a == 1) ? 20: 30;
 instanceof Operator
• Syntax:
( Object reference variable ) instanceof (class/interface type)
• Example:
boolean result = name instanceof String;

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Precedence of Java Operators
Category Operator Associativity
Postfix () [] . (dot operator) Left to right
Unary ++ - - ! ~ Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift >> >>> << Left to right
Relational > >= < <= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right

Unit-1
Unit-1 Introduction
Introduction Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology

Vous aimerez peut-être aussi