• No results found

core java

N/A
N/A
Protected

Academic year: 2021

Share "core java"

Copied!
60
0
0

Loading.... (view fulltext now)

Full text

(1)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Introduction

Java is Simple high Level ,Platform independent , Architecture Neutral, Secure, Robust ,Multithreaded, Distributed, and Object Oriented programming language.

Java Characteristics /Feature/Buzzwords

• Simple

Java is easy to learn because most of the complex or confusing feature of c and C++ like pointers , operator overloading etc are not provided in java.

• Secure

Java programs run within the JVM which protects from unauthorized or illegal access to system resources.

• Platform independent/pointer

Java program can be executed on any kind of machine containing any cpu or any operating system.

• Architecture neutral

Since java application can run on any kind of CPU so it is architecture – neutral.

• Robust

 Java is robust because of following..

• Strong memory management

• No pointer

• Exception handling

• Type checking mechanism

• Platform independent

 Multithreaded

Java supports multithreaded programming which allows us to write programs that do many things simultaneously

• Distributed

Using RMI and EJB we can create distributed application in java

• Object oriented

Java follows object oriented programming model that helps to break the complex code into easy to understand objects

(2)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

• Language

It is the medium of communication .

• program

It is a set of instruction for a device to perform to some specific task.

• Programming Language

It contain set of grammatical rules or syntax to write a program for instructing machine to perform specific tasks.

Types of Programming Language

1. Low Level Programming 2. Middle Level Programming 3. High Level Programming

Low Level Programming Language

 Binary digits (0,1) will be used to write the instruction in Low Level Language

 Instruction write in binary language or native language will be understand by the machine directly .  No transfer program is required.

PROBLAM

• It is very complex to write ,modify and debug the instruction written in Binary Language .

• Binary code or Native code is platform dependent.

Middle Level Programming

 Assembly codes like ADD ,SUB,MUL,DIV,MOV etc .Will be used to write the instruction in Middle Level Language .

 Instruction written in assembly language will not be understand by the machine directly .

 Assembler is a program that should be used to convert Assembly Language To Machine Language.

PROBLAM

• The code written in Assembly Language will be processor dependent.

High Level Programming

 Human understand codes like English words will be used to write the instruction in High Level Language .

(3)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

 Computer or interpreter is a program that should be used to converter High Level Language to Machine Language.

Advantage

 it is easy to write ,modify and the instruction written using High Level Language.

Hello.c

WINDOWS MAC OS LINUX

C Language Program Development and Exception Model

Working on Window Platform

C Source Code

C Compiler

Machine Code Or Native Code

Will be executed Successfully Will not be executed Successfully Will not be executed Successfully

(4)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL Hello.java Hello.class

WINDOWS MAC OS LINUX

1.

What is Source Code

java Language Program Development and Exception Model

Working on Window Platform

java Source Code

Java Compiler

Byte Code

JVM JVM JVM

Native Code Native Code Native Code

Will be executed Successfully Will be executed Successfully Will be executed Successfully

(5)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Human understandable code written using High Level Programming language is called as Source code.

2.

What is native code or Binary code or Machine code

Machine understandable code written using Low Level programming Language is called as Native code.

3.

What is byte code?

JVM understandable code generated by java compiler is called as Byte code. Byte code is also called as Magic value .

4.

Why C and C++ is platform Depedent?

When you compile c or C++ program on one operation System then compiler generates that operating system understand native code. This native code generated on one os will not run on other os diretly.

5.

Why java is platform independent?

When you compile java program on one Operating system then java compiler generates Byte code. You can run that Byte code on any os which has the JVM.

JVM is understanding the Byte code and generating native code for the corresponding os.

Because of Byte code and JVM , java is platform independent you can use – write once Run Anywhere(WORE).

6.

I have written one program in c or c++ and onather program

in java . Which program will be executed fastly and why?

C or C++ program execution is faster than java program.

Because compiled code of c or c++ program contains binary code and that binary code will be executed directly whereas compiled code of java program contain byte code and that byte code will be converted to native code first and then native code will be executed.

7.

What is java compiler?

Java compiler is a program developed in C or C++ programming language with the name “javac” . it is responsible for the following tasks.

1->It will check syntactical or grammatical errors of the programs . 2->It converts source code to byte code.

8.

What is java interpreter?

Java interpreter is a program developed in C or C++ programming language with the name “java”. It is responsible for the following tasks.

1 It will convert byte code to native code line by line. 2 It will execute that native code.

NOTE Because of converting and executing line by line, java program execution was slower in initial versions of java.

9.

What is jit compiler?

(6)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

JIT compiler compiles or translates or converts the necessary part of the byte code into machine code instead of converting line by line . Because of this performance of java program has improved.

10.

What is JDK( java Development Kit)/SDK(software

Development Kit)?

It is a set of various programs which are required for developing and executed the java programs. It is a platform dependent. Various JDKs are provided for various Operating system.

Following are various utility programs provided under JDK.

1) Java Development Tools

• Javac • Java • Javap • Jar • Etc • Source File • JRE • Etc •

What is JRE ?

JRE stands for java runtime Environment . It is an implementation of JVM . It contain class Libraries, interpreter, JIT Compiler etc. Only JRE enough to run the java program.

11.

What is JVM?

JVM stands for java Virtual Machine . It is a specification provided by SUN Microsystem whose implementation provides an environmeint to run our java applications. JVM because aninstance of JRE at run time.

Sun’s implementations of the java Virtual Machine(JVM) is itself called as JRE . Sun’s JRE is available as a part of JDK and also as a separate application.

Many vendors has implemented JVM. Some of them are SUN JRE , IBM JRE ,Oracle JRE etc.

JDK

Hello.java

Development Tools

Java Runtime Environment/java Virtual Machine

(7)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL Hello.class

Class Files Class Loader Byte Code Verifier Security manager

Method Heap Stack PC Other

Area Memory Memory Register Components

Execution JIT GC Engine Compiler 0 1 0 1 0 1 0 0 1 0 1 0 0 1

Operation System/Platform

Hardware

(8)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

History

• Java was designed by sun Microsystems in the early 1990s to solve the problem of connection many household machines together.

• Initial version of java is called as OAK.

• Later java become popular for developing internet applications .

• Java is developed by James Gosling and team.

• James Gosling is considered as FATHER OF JAVA.

• It was publicly released on 1995.

• Now sun Microsystems is the part of Oracle Crop.

Editions

• Java Standards Edition(JSE)

Used to develop standalone applications using applet and swing.

• Java Enterprise Edition(JEE)

Used to develop enterprise application using Servlets,jsp,jdbc etc.

• Java Micro Edition(JME)

Used to develop applications for micro devices like Mobiles , Setup Box etc.

Java Standard Edition Versions

Java Version

Code Name

Released Year

JDK Alpha and Beta 1995

JDK 1.0 Oak 1996 JDK 1.1 1997 J2SE 1.2 Playground 1998 J2SE 1.3 Kestrel 2000 J2SE 1.4 Merlin 2002 Enterprise Edition(EE) Standerd Edition(SE) Micro Edition(ME)

(9)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL J2SE 5.0 Tiger 2004 J2SE 6 Mustang 2006 JAVA SE 7 Dolphin 2011 JAVA SE 8 18 march 2014

Java Enterprise Edition Version

EE version

Released Year

Technologies Added

Java(java Professional)

1998

JDBC 2.0

JNDI 1.2 Servlets 2.2 Jsp1.1 EJB 1.1

Java Mail API1.1

J2EE1.3

2001

Servlets 2.3

JSP1.2 EJB2.0

Java Mail API 1.2

J2EE1.4

2003

Servlets 2.4

JSP 2.0 EJB 2.1

Java Mail API1.3

JEE 5

2006

Servlets 2.5

JSP 2.1 JSF 1.2 EJB3.0 JPA1.0

Java Mail API1.4

JEE 6

2009

JDBC 4.0 Servlets 3.0 JSP 2.2 JSF 2.0 EJB 3.1 JPA2.0

JEE 7

2013

JDBC 4.1 Servlets 3.1 JSP 2.3 JSF 2.2 EJB 3.2 JPA2.1

(10)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Installation step

• Run the installer file JDK -7u25-windows-i586.exe

• Click on NEXT button

• Clock on Change button.

• Change the destination location to E\jdk\1.7.0\

• Click on OK button.

• Click on NEXT button .

• After some time installer will prompt to select destination location for JRE.

• Use the defaults as it is and click on NEXT button then following screen will appear.

• After installation completed successfully.

• Click on close button.

Directory Structure

• After installing JDK you will get the following directory structure.

JDK Directory

JRE Derectory

E:

lib

C:

Program Files

Jdk 1.7.0

bin

jre

Srz.zip

java

Jre7

Javac ,, jar.exe Java.exe ,,etc Javap Private Jre Public jre

(11)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Step to write , compile , and Run Program Step to write java Program

Select the required Workspace

Assume we have select workspace as D:\ Mukesh \Ankush

Use Notepad or some other text editor to create the source file.

Steps

Click to start ->Run.

Type notepad and click OK.

Write a java program.

Sava the file with the name of Hello.java in the directory D:\ Mukesh\ Ankush.

Step to compile the program

1.

Open Command Prompt as Follows

• Click on Start -> Run

• Type cmd • Click on OK JDK Directory Workspace E: Jdk 1.7.0 bin Java.exe,java.exe Java.exe,javap.exe Jar.exe,,etc D: Mukesh Ankush Hello.java

(12)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

2. Change working Directory to Workspace Directory (D:\Mukesh\Ankush)

3. Set the Path for bin directory of JDK.

D:\Mukesh\Ankush>set path =%path%;E:\JDK 1.7.0\bin;

4. Use javac compiler to compile the source code. Javac <sourceFileName With .java Ext>

Compiler will generate Hello.class file in the location D:\Mukesh\Ankush

Step to Run java Program

• Use java interpreter to execute/run the java class

Java <classFileNameWithout.class Ext>

If you set the path at command line then that path will be accessed as long as that command prompt running .Even that path will not be accessed with other prompt is running at a time. So it is temporary.

You can set the PATH permanently in System Environment Variables as follows.

• Right click on My Computer and Select Properties.

• Select Advanced Tab.

• Click on Environment Variables Button.

• Click on new Button in the User Variable

• Provide the required info.

Variable the required info. PATH

Variable value E:\jdk1.7.0\bin

(13)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

COMMENTS

Single Line Comments (//)

// the whole line will be ignored by the compiler.

Documents Comments

/** will be used to

*define documentation

*of any variable, method

*in java Source File.

*/

Multiple Line Comments(/* */)

/* you can define as

Many lines as

You want to

Ignore from compilation.

*/

What is the difference between c++ and java?

java

C++

Java doesn’t support pointer

C++ support pointer

Java class not support multiple

inheritance with classes.

C++ supports multiple

inheritance with classes.

Java doesn’t support Global

Variable (variables declared

outside the class)

C++ supports Global Variables.

Java doesn’t support header

files.

(14)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Const and goto keywords can’t

used in java .

Const and goto keywords can be

used in c++.

Java doesn’t supports destructor C++ support Destructor

Java doesn’t support virtual

function

C++ support virtual function.

Java doesn’t support operator

overloading .

C++ support operator

Overloading.

Java doesn’t support scope

Resolution Operator.

C++ support scope resolution

operator.

Java has a garbage collector to

clean the memory automatically.

No automatic memory cleaning

process in c++.

Java has built- in API for

multithreading and network

Programming

C++ doesn’t have Built- in API for

multithreading and network

Programming

(15)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Java

Language Ref

Guide

Part 1

(16)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Java Language Fundamental

Character Sets

Keywords/Reserved words

Identifier /User Defined Words

Data types

Variable

Constants

Literals

Operators

Control Statements

Arrays

1.

Character sets

Alphabets A-Z/a-z. Digits 0-9

Special Characters +-*&! @#$%^ etc

White Space character newline (\n), carriage return (\r) etc

2.

Keywords/Reserved words

Keywords are simple

E

nglish words which are having predefined meaning in java programming Language.

Meaning of these keywords can’t be modified.

Keywords are also called as Reserved Words.

All the keywords are defined in Lower Case.

We can’t use keywords as names for variables, methods, classes, or as any other identifiers.

(17)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

There are 49 keywords defined up to java 2.

enum is the new

keyword added from java 5 so there are 50 keywords from java 5.

const & goto are the keywords but no implementation available. You can’t use these keywords in java program.

List of Keywords

Data types(8)

boolean ,byte ,chat ,short, int

,float, double

Class & object(9)

class , interface , enum,

extends, implements , this ,

supper, new.

Access Modifier(3)

Private, protected ,public

Modifier(9)

final , native, abstract,

synchronized ,transient,

volatile ,static ,const*

Package(2)

package, import

Control Statements(12)

if ,else ,switch, case, defaults

,do ,while, break, for,

continue, return

Exception Handling(6)

try , catch, finally, throw,

throws

Other data Type(1)

void

3.

Identifier /user Defined Words

Identifiers are the names those will be used to identify the programming elements like classes, methods, variables, etc uniquely.

(18)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Rules to follow when you define a identifiers



Identifiers can

contain Alphabets, Digits, and two special symbol i.e Doller ($), Underscore (_).



First character of an identifier must be an Alphaber or Doller ($)

or Underscore (_).



Keywords or Reserved words can’t be used as Identifier.

Valid Identifiers

Invalid Identifiers

Hello 1stClass

Int true

getClassName student number

Total_FFE int

4.

Data types



Data type represents

Type of data you want to use

Amount of memory allocation required for your data.



There are two types of data types.

Primitive data types

User defined data types.

Primitive data types

• There are 8 primitive types available in java.

• 8 keywords are defined to represent 8 primitive data types.



Following are primitive data types.

(19)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

User Defined Data type

 There are four types of user Defined Data types.

•Class type

•Interface type

•Enum type(from java 5)

•Annotation type(from java 5)

Primitive Types Floating Point Types Integral Types Numeric Types

Boolean Types boolean

byte short float double char long int

(20)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Type

Size

Default

value

Min value

Max value

Byte Bits

boolean N/D N/D false True or false

Byte 1 8 0 -27(128) 27-1(127) Char 2 16 ASCII-0 Unicode-\u0000 0 216-1 (65535) short 2 16 0 -215 (-32,768) 215-1 (32,767) Int 4 32 0 -231 (-2147483648) 231-1 (2147483647) Long 8 64 0 -263 263-1

Float 4 32 0.0 1.4E-45 3.40E38 Double 8 64 0.0 4.9E-324 1.79E308 Any

reference type

8 64 Null Reference of The

corresponding Type object

5.

Variable

• Variable is the container which holds user data.

• Memory will be allocated for the variable while executing the program.

• Value of the variable can be changed any number of times during the program execution.

Syntax

<Data type> <varName>;

<Data type> <varName> =<value>;

EX

Int a; String str; a str

(21)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Int a=99; String str =”SRI”; a str

4 bytes 8 bytes

32597

Types of Varialves

 There are two types of variable based on data type used to declare the variable.

• Primitive Variables

• Reference Variables

Primtive Variables

Variable declared with primitive data type are called as primitive variable.

EX

Int a; Int b; Double d1; Double d2=9.9;

Reference Variables

• Variable declared with user defined data types are called ass reference variables.

EX

String str1; String str2=”Ankush” ;

99

32597

(22)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Primitive Variable

Reference Variable

Variable declared with primitive data types are called as primitive variable

Variable declared with reference data types are called as reference variables.

Memory allocation for primitive variables depends on the primitive data types

Always 8 byte of memory will be allocated for reference variables.

Default value for primitive variables depends on primitive data types

Always null will assigned as default value for reference variables.

Primitive variables hold valid literals or value in the allocated memory.

Reference variables hold either null or address of an object in the allocated memory.

Types of variables

 There are three types of variables based on the scope of the variables.

• Instance Variables.

• Static Variables.

• Local Variables.

Instance Variables

• Variables declared in the class without using static keywords are called as instance Variables.

Static Variables

Variables declared in the class using static keyword are called as Static Varaibles

Local Variables

• Variables declared in the member of the class like method etc are called as Local variables.

(23)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Class Hello {

Int a; instance variable

Static int b; static variable

void show(){

Int c; local variable

}

}

There a

re various syntax for declaring variables

Declaring a single variable in one variable declaration statement.

Syntax

[modifiers]<data type> <varName>

;

EX byte b

;

int a

;

double d

;

char ch

;

String str

;

Hello h

;

Etc

(24)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Initializing a single variable

syntax

;

<varName> = <value/expression>

;

EX

b=24

;

ab=98765;

d=1234.34

;

ch=’A’

;

str=”Ankush”

;

h=new Hello()

;

Declaring multiple variables of single type in one variable declaration statement.

Syntax [modifiers]<data type><var1>,<var2>,<var3>,…..

;

EX

byte b1,b2,b3,b4

;

int a,b,c

;

char ch1,cj2,ch3

;

String str1.str2,str3

;

(25)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Syntax

<var1>=<var2>=<var3>=<value/expression>; EX a=b=c=9898

;

str1=str1=str3=”Ankush” ;

Declaring and initializing multiple variable of single type in one

variable declaration statement.

Syntax

[modifiers] <data type><var1>=<val1>,<var2>=<val2>,<var3>=<val3>…….

;

EX

Int a=120, b=54,c=98;

String str1=”Ankush”,str2=”Mukesh” ;

boolean b1=true,boolean b2=false;

6.

Constant

Constant is a special variable whose value can’t be modified during the program execution.

Constant is also called as final variable.

Syntax

[modifier]final <data Type> <varName> = <value>

;

EX

(26)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Final String str=”Ankush” ; Final double d1=9999.99;

• A Variable that is declared as final and not initialized is called as blank final variable.

Syntax

[modifier] final <data type> <varName>

;

<varName>=<value> ;

EX

final int a

;

a=98;

• If the variable is declared as final and initialized in the same statement then in the class final compiler will replace that variable with actual value.

Default value of char is either ASCII-0 or UNICODE\u0000. Default value of char is not space.

JVM will not provide default value for Local Variable. Local variable must be initialized before using.

The local variable declared but not initialized and not used then that variable declaration statement will be removed by compiler while translation to byte code.

JVM will provide default value for static Variable

Static variable can be accessed from Static method(main method) JVM will provide default value for instance Variable

Instance variable cannot be accessed directly from Static method .

(27)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

You can’t declare multiple variables of different data types in single variable declaration statement

You can declare multiple variables of same data types in a single statement You can assign same value of multiple variables in a single statement

You can declare and initialize multiple variables of same data type in a single statement

We can’t declare two

variable with same name in same scope

Value of the variable can be changed any number of times during program

execution

Value of the final variable can not be changed We can’t use const keyword to declare constant

String str1=”Ankush”,str2=”Mukesh” ;

• Declares variable str1.

• Assigns the value Ankush to variable str1.

• Declares variable str2

• Assign the value Mukesh to variable str2

Int a,b

;

Int c=a=b=99;

Declares a new variable c

Assigns value 99 to variable b

Assign value of variable b to variable a

Assigns value of variable a to newly declared variable c

int a=a=99

;

declares variable a

assign value 99 to variable a

(28)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Int a=b,b=99

;//invalid

Declares variable a

Trying to assign value of variable b to variable a

7 .Literals

• Literals are the actual values

• Literals will be assigned to variables or constant

• Literals are also used to perform any operation

• Constant values in program are called as literals

Data type Variable Name Literals

Int a= 123;

Data type Variable Name Literals

(29)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Types of Literals

Boolean Literals Character Literals String Literals Integral Literals Floating Literals null Literals

Boolean Literals

• boolean Literals can be assigned to a boolean type variables

• there are two boolean literals  true  false

Character Literals

Numeric

Literals

Non Numeric boolean

String

Unsigned

Signed

(30)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

• Character Literals is a single character enclosed within a single quotation mark.

• Character Literals can be assigned to char type variable



Escape sequence



ESCAPE SEQUENCE

is a special notion is used to represent some special

characters which can’t be represented as it is.

Escape Sequence

Description

\t

Tab space

\b

Back space

\n

New line

\r

Carriage return

\f

Formfeed

\’

Single quote character

\”

Double quote character

\\

Backslash character



ASCII Character sets



ASCII stands

for American Standard Code for information Interchange.



Every character enclosed in single quotation marks will have an integer equivalent value called as ASCII value.



ASCII value Range is 0-255



ASCII value can be assigned to char type variable.



Unicode character



UNICODE stands for UNIversal Code



Every character will have UBICODE value.

UNICODE Notation

Syntax

(31)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

UNICODE Range

\u0000 (0) to \uFFFF(65535)

Octal value as char type

Octal Notation

Syntax

\DDD -D will be octal digits

Start with \ following by three octal digits Octal must be enclosed in single quotes

Octal Range

Range in Decimal 0 - 255 UNICODE 30 means \u0030 and so on Range in octal \0 - \377

Char

ASCII

OCTAL

UNIC

Char

ASCII

OCTAL

UNIC

0

1

2

3

4

5

6

7

8

9

:

;

48

49

50

51

52

53

54

55

56

57

58

59

060

061

062

063

064

065

066

067

070

071

072

073

30

31

32

33

34

35

36

37

38

39

3a

3b

<

=

>

?

@

A

B

C

D

E

F

G

60

61

62

63

64

65

66

67

68

69

70

71

074

075

076

077

100

101

102

103

104

105

106

107

3c

3d

3e

3f

40

41

42

43

44

45

46

47

(32)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

a

b

c

d

e

f

g

h

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

97

98

99

100

101

102

103

104

110

111

112

113

114

115

116

117

120

121

122

123

124

125

126

127

130

131

132

141

142

143

144

145

146

147

150

48

49

4a

4b

4c

4d

4e

4f

50

51

52

53

54

55

56

57

58

59

5a

61

62

63

64

65

66

67

68

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

151

152

153

154

155

156

157

160

161

162

163

164

165

166

167

170

171

172

69

6a

6b

6c

6d

6e

6f

70

71

72

73

74

75

76

77

78

79

7a

(33)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

String Literals

String literals is a collection of zero or more characters enclosed between double quotation marks.

String Literals can be assigned to reference variable of type String

String Literals will be represented as arrays In memory.

Integer Literals

 Integer Literals can be assigned to one of the integral data type –byte, short , int, long

 Integer Literal size is depending on integral data type you are using  Default type of integer Literal is int

 There are four types of integer Literals

• Decimal literals

• Octal Literals

• Hexadecimal Literals

• Binary Literals(from java 7)

Decimal Literals

Decimal Literals is a valid number from decimal number system

The base or radix of decimal number system is 10 i.e only 10 digits ranging from 0 to 9 are allowed to from the decimal Literals

Decimal Literals must not start with 0

(34)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL



98875437

Octal Literals

• Octal Literal is a valid number from octal number system

• The base or radix of octal number system is 8 digits ranging from 0 to 7 are allowed to from the octal literals

• Octal Literals must start with 0(zero)

hexadecimal Literals

Hexadecimal Literal is a valid number from hexadecimal number system

The base or radix of hexadecimal number system is 16 i.e only 16 digits

ranging from 0 to 9

And A/a to F/f are allowed to from the hexadecimal Literals

Hexadecimal Literals must start with 0X/0x.

Binary Literals (from java 7)

• Binary Literals is a valid number from binary number system

• The base or radix of binary number system is 2 i.e only 2 digits 0 and 1 are allowed to form the binary Literals

• Binary literals must start with 0B/0b  0b1010

 0B111010101010

Floating point Literals

• Floating point Literal can be assigned to one of the floating point data type –float, double .

• Floating point Literal size is depending on floating point data type you are using

• Default type of floating point Literals is double

• You can use D/d as a suffix for the double value optionally.

(35)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

• Floating point Literals can be represented using following notations  Standard floating point notation

 Exponent / scientific notation  Hexadecimal Notation (from java 5)

Decimal point 1/10 1/100 Tens Units 1/1000

12.876

10x Bigger 10x smaller

Standard Floating point Notation

It consists of a whole number followed by a decimal point and fractional part 1234.76

2345.7609

Scientific / Exponent Notation

• The exponent is indicated by an E or e following number, which can be positive or negative

123.4567e2 ->123.4567*102 123.4567E2 ->123.4567*102

12345.67e-2 ->12345.67

*102

Hexadecimal floating literals (from java 5)

• It allows literals of the float and double types to be written primarily in base 16 rather than base 10.

(36)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Double x =0xap0;

a->10 //10*20=10.0

Double y =0xfp2D;

f->15 //15*22=60.0

float z =0xf.ap1F;

f->15 a->10 //(15*160+10/161)*21 //(15*1+.625)*2 //15.625*2)=31.25f

Double d1=0x1f.a5p1;

//(1*161+15*160+10/161+5/162)*21 //(16+15+10/16+15/256)*21 //(16+15+.625+.01953125)*2 //(31.64453125)*2 //63.2890625

null Literals

null is a value

it is default value for any reference variable

if the value of reference variable is null then it indicates that address/reference is not available in the variable

UNDERSCORE in numeric Literals(from java 7)



to represent the unit of the values you can use comma (,) symbol in number representation

EX

(37)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL



you can do the same task with UNDERSCORE (_) from java 7 towards

EX

2_45_12_452

1)

A boolean type variable can hold either true or false

2)

A boolean type variable can’t hold numeric value



0 not equals to false



Non zero values not equals to true

3)

Boolean literals – true and false are reserved words and must be in lower case



true not same as True TRUE, True, TrUe etc



false not same as FALSE ,False, FaLse etc

4)

a char type variable can hold following



Single character enclosed in single quotation marks



Escape sequence



ASCII value



UNICODE character

5)

SPACE is valid character so it can be placed in single quotation marks

6)

TAB is valid character so it can be placed in single quotation marks

7) Back space character (\) is used to form escape sequence so only backspace

character can’t be placed in single quotation marks

8) Single quote is used to form character literal so only single quote can’t be

placed in single quotation marks

(38)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

9) There are some character like backslash, single quote which can’t be placed

in single quotes as it is. those special characters have to be represented as

escape sequence

10)

Every character enclosed in single quotation marks will have an integer

equivalent value called as ASCII value

11)

Char type variable can hold integer value ranging form 0-65535 only

12)

Char type variable can hold UNICODE value ranging from \u0000-\uFFFF

only

13)

Char type variable can hold OCTAL value ranging from \0-\377 only

14)

When you print he char type variable which holds integer, UNICOE or

octal value then it displays corresponding character representation

15)

If integer , UNICODE value don’t have character representation then it

displays? Which indicates no character defined for that value.

16)

When you assign character literals to int type variable then internally

ASCII value of that character will be assigned.

17)

Source code can also be written in UNICODE representation

Int a=\u0037; int a=7; //valid

Int a=’\u0039’; int a=’7’; //valid

Int a=\u0037\u0037; int a=79; //valid

Int a=’\u0037\u0037’ int a=’79’; //invalid

18)

Empty string Literal is allowed whereas empty character literal is not

allowed

19)

If reference variable of String type contains null value then it is called as

NULL String

(39)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

20)

You can’t refer any member with NULL String . if you do so it results in

NullPointerException

21)

If reference variable of String type contains zero character in double

quotations marks then it is called as EMPTY String .

22)

When java compiler encounters any UNICODE/Octal value in String

literals then that UNICODE/Octal value will be replaced with actual

character

23)

You can use escape sequence to represent UNICODE/Octal value as it is

in String literals

24)

There are four types of integer literals

Decimal Literals

Octal V Literals

Hexadecimal Literals

Binary Literals(from java 7)

25)

Default type if integer Literal is int

26)

Integer value ranging from -2147483648 to 2147483647 can be

represented directly

27)

If you want to represent the integer value out of int range then you need

to use L/l ssuffix

28)

Decimal literals must not start with 0

29)

Octal Literal must start with 0(zero)

30)

Hexadecimal literal must start with 0X/0x

31)

Binary literal must start with 0B/0b

32)

When java compiler encounters octal , Binary or Hexadecimal Literals

then that will be replaced with decimal equivalent value

(40)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

34)

You can use D/d as a suffix for the double value optionally

35)

You must use F/f as a suffix for float value

36)

When integer value starts with 0 then it will be considered as octal

literals

37)

When floating point value starts with 0 then it will be considered as

floating point literals only , not as octal literals

38)

When integer value starts with 0X/0x then it will be considered as

Hexadecimal literals

39)

When floating point value starts with 0X/0x then it must use p in

fractional part

40)

When integer value starts with 0B/0b then it will be considered as binary

literals

41)

Floating point cannot be represented as Binary Literal

42)

You can use UNDERSCORE in numeric Literals to represent unit of values

from java 7

43)

When UNDERSCORE is used before first digit then it will be considered as

a variable

44)

You can’t use UNDERSCORE after the last digit

45)

In the case of octal literal UNDERSCORE can be used just after 0

46)

In the case of binary literal UNDERSCORE can’t be used just after the

0B/0b

47)

In the case Hexadecimal literal UNDERSCORE can’t used just after 0X/0x

48)

You can use more than one UNDERSCORE side by side between two

digits

49)

You can use UNDERSCORE with floating point literal but not allowed just

before or after the decimal point

(41)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Part -2

Operator

• Operator at used to perform operations by using operands

• There are three types of operator depending on the number of operands required  Unary operator –only one operand is required.

 Binary operator-two operands are required  Ternary operator- three operands are required

Following are the types of operator depending on the operation

• Arithmetic operators

 Unary arithmetic operator  Binary arithmetic opetator

• String concatenation operator

• Assignment operator

 Simple assignment operator  Compound assignment operator

• Increment & decrement operator

• Relational operators

• Logical operator

• New operator

• New operator

• Instance operator

• Conditional or Ternary operator

(42)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Arithmetic operator

• Arithmetic operator are used to form arithmetic expression to perform the arithmetic operations.

• Operands used in arithmetic expression can be of numeric type or char type

• Result of arithmetic type operation is always int or wider than int type.

Operand1 operator operand2

10 + 20

Unary Arithmetic operators

Operator

Name

description

+

plus

Return same value

with same sign

-

Minus

Return same value with

opposite sign

Binary Arithmetic operators

Operator

Description

+

Addition(sum)

-

Subtraction(difference)

*

Multiplication(product)

/

Division(quotient)

%

Modulus(remainder)

(43)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

When the operands are int or lower than int type then result will be

int type only.

When any one operand or both the operands are wider than type

then result will be wider type.

1

st

operand type

2

nd

operand

Result type

byte

byte

int

char

char

int

short

byte

int

int

int

int

int

long

Long

byte

double

double

float

int

float

float

long

float

char

double

double

int

double

double

char

int

int

short

char

int

short

float

float

etc

String Concatenation Operator(+)

It is a binary operator

+ operator can be used for two purposes



Arithmetic addition

(44)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Arithmetic Addition



When both the operands of + operator are numeric type

then it will perform arithmetic addition

EX

90 + 9 = 99;

Result of arithmetic addition is numeric type

• String concatenation

 When any one or both the operands of + operator are String type it will perform String concatenation.

EX

“90” + 9 = 909 9 + “90” = 909 “90” + “9” =909

Result of String concatenation is String type.

Simple Assignment Operator(=)

• it is binary operator

• it is used to assign the value to a variable  <operand1> = <operand2>

Operand1 must be a variable

Operand2 can be a variable, value or expression.

• The variable , value or value of an expression must be assignment compatible type.

byte

char short

(45)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Type Casting

• <Operand1> = <operand2>

• Here operand2 is the source , operand1 is destination and we are assigning source to destination.

• Usually source type must be same as destination type

• When source type is not same as destination type then source type must be converted to destination type . This process of converting one type to another is called as Type casting.

• Type casting can be done in two ways.  Implicit casting

 Explicit casting  Implicit casting

 When type casting is happening automatically by the JVM then it is called as Implicit Casting.

 Explicit casting

 When type casting is done by the programmer explicitly then it is called as Explicit Casting.

SYNTAX

<dest Type><dest var> = (<dest type>)<source value> EX

Int a=10; byte b= (byte)a;

There are two type of conversions



Widening



Narrowing

Widening



Widening is the process of converting lower type to higher type . this is safe conversion

(46)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

EX

byte b=10;

int a= b; //valid - implicit casting int a= (int )b; //valid – Explicit casting

here you are casting byte to int . byte has a range from -128 to 127 where as int has a range from -2147483648 to 2147483647 . so any value that fits in a byte will fit in a int also . because of this there is no data loss and conversion is considered as sage.

Narrowing

 Narrowing is the process of converting higher type to lower type

EX

Int a=300;

byte b=a; //invalid implicit casting byte b=(byte)a; //valid explicit casting

here you are casting int to byte . Range of byte is much smaller than range of int . what happens when you convert an int value 300 to a byte.

This result 44 i.e data is lost so conversion is considered as unsafe. Int a=300; ->int means 32 bits memory required

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 0

byte b1=(byte)a; ->byte means 8 byte memory required 0 0 1 0 1 1 0 0

(47)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL 0*27+0*26+1*24+0*23+1*22+0*21+0*20 0+0+32+0+8+4+0+0=44 double val 300.9898; byte b2=(byte)val; =44.

Compound Assignment Operators

• Compound assignment operator provide a shorter syntax for performing

operations and assigning the result of an arithmetic operator or bitwise operator

EX

int a=90;

a+ = 9; => a = a+9;

• In the above expression two takes are happing

 Performs the Arithmetic operation on the operads.  Assigns the result to the first operands

• There are 11 compound assignment operators as shown below

Operator

description

+= Add right operand to left operand and

assign the result to left operand.

-= Subtract right operand from left operand and assign the result to left operand

*= multiple right operand to left operand and assign the result to left operand

/= divide right operand to left operand and assign the result to left operand

%= Calculate modulus using two operands

and assign the result to left operand &= assigns the result of the logical AND |= assigns the result of the logical OR

(48)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

^= assigns the result of the logical XOR <<= assigns the result of the signed left shift

operator

>>= assigns the result of the signed right shift operator

>>>= assigns the result of the unsigned right shift operator

Increment (++) / Decrement (--) Operators

• Increment operator will be used to increase the value of the variable by 1

• Decrement operator will be used t decrease the value of the variable by 1

Relational operators

• These are binary operators

• Relation operators forms the relational expressions

• Result of relation expression is Boolean type i.e true or false

Comparison Operators

description

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

• Operands of comparison operators must be number or char type

Equality operators

Description

== Equal to

!= Not equal to

• Operands of equality operators can be numeric , char, boolean or reference type.

Logical operators

• Logical operators are used to form logical expressions

(49)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

• Result of logical expression will be boolean type i.e true or false

Logical operator Description

! Logical NOT

&& Logical AND

|| Logical OR

Logical NOT

• It is unary operator

• It is used to inverse the result

OPERAND Result

true false

false true

Logical OR and Logical AND

• These are binary operators

• Logical OR and Logical AND operators used to combine two relational expressions to from logical expressions

EX

(a>b) && (a>c) (a<b) || (a<c)

Logical AND (&&)

Result of logical AND is true if both the operands are true otherwise false

OP1

OP2

Result (op1 && Op2)

true

true

true

true

false

false

false

true

false

false

false

false

(50)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

Result of Logical OR is false if both the operands are falseotherwise true.

OP1

OP2

Result (op1 || Op2)

true

true

true

true

false

true

false

true

true

false

false

false

New operator

• new operator is used to create the new object for class

• it returns of the address of the address of newly created object

Instanceof Operator

• it is used to check whether the given object belongs to specified class or not

• Result of instanceof operator is Boolean value

• It is also called as TYPE COMPARISION OPERATOR

Conditional operator

• It is ternary operator

<Operand1> ? <Operand > : <Operand3>

• Operand1 must be of boolean type

• If operand1 is true then Operand2 will be returned otherwise Opetand3 will be returned

Bitwise Operators

• These operators will be used to perform the operation on individual bits of the value.

Bitwise Operators

Description

(51)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

& Bitwise AND

| Bitwise OR

^ Exclusive OR (XOR)

<< Left Shift

>> Right shift

>>> Unsigned Right Shift

• Operands for the following Bitwise Operators can be of boolean type or integer type .

 Bitwise AND  Bitwise OR

 Exclusive OR (XOR)

• Operands for the following Bitwise Operands will be of Integer type only  Bitwise NOT

 Left Shift  Right shift

 Unsigned Right Shift

1’s Complement

• 1’s complement if a binary number is formed by changing 1’s to 0’s and 0’s to 1’s

2’s Complement

2’s complement of a binary number is formed by adding1 to the least significant bit of 1’s complement

0 0 1 0 1 1 0 1

1 1 0 1 0 0 1 0

Actual Bits

(52)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

0

0

1

0

1

1

0

1

1

1

0

1

0

0

1

0

+

1

1

1

0

1

0

0

1

1

Bitwise NOT Operator

It inverts the bit pattern and returns decimal equivalent value.

This is equivalent to 1’s complement Int x=15; Sign bit

64 32 16 8

4

2

1

0 0

0 0

1

1

1

1

1

1

1 1

0

0

0

0

Int x =45

; Sign bit

64 32 16 8

4

2

1

0 0

1 0

1

1

0

1

1

1

0 1

0

0

1

0

Shortcut method

~OPERAND = -(OPERAND + 1) Actual Bits 1’s Complement

Adding 1 with value

2’s Complement

Binary of 15

1’s Complement (~x)=-16

Binary of 45

(53)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

EX

~198 => -(198 + 1)=> -199 ~-198 => -(-198 +1) => -(-197) => 197

Bitwise operator

• Bitwise AND operator is similar to Logical AND operator with following differences

Logical AND

Bitwise AND

Operands are Boolean type only Operands can be Boolean as well as integra type

When first expression value is false then second expression will not be evaluated

When first expression value is false then second expression also will be evaluated Result is Boolean type When operands are Boolean type then

result is boolean type

When operands are integral type then result is integral type.

OP1

OP2

Result (Op1 & Op2)

1 1 1

1 0 0

0 1 0

0 0 0

Bitwise OR Operator

• Bitwise OR operator is similar to logical OR Operator with following defferences.

Logical OR

Bitwise OR

(54)

J

AVA

L

EARNING

C

ENTER

BY-A.K.JAISWAL

type When first expression value is true then

second expression will not be evaluated

When first expression value is true then second expression also will be evaluated Result is Boolean type When operands are Boolean type then

result is boolean type

When operands are integral type then result is integral type.

OP1

OP2

Result (Op1 & Op2)

1 1 1

1 0 0

0 1 1

0 0 0

Exclusive OR Operator (XOR)

• Operands can be Boolean as well as integral type

• Both the expressions are Boolean type then result is Boolean type

• When operands are Boolean type then result is Boolean type

• When operands are integral type then result is integral type

OP1

OP2

Result (Op1 ^ Op2)

1 1 0

1 0 1

0 1 1

0 0 0

Left shift Operator (<<)

References

Related documents

Makes it can use string methods java examples given index of a string is an alternative to know how it returns boolean datatype that represents the starting the most used..

Java String code point methods are used to record the Unicode Code Points of is character right the string.. Most software testers know about today for

In Java, a String object encapsulates a sequence of Unicode (see http://www.unicode.org) characters and provides a number of methods that allow programmers to use strings

Variables of type double, boolean, and String can be declared interactively and must be declared within a Java

Java class in this output statement example below displays all such a string with java programs output string examples pdf or a string objects in java.. JAVA for Beginners Search

The string object that perform mathematical operations like numbers, if we can access instance data type and initialization using this tutorial will learn about a colon between

• The Convert class (not part of standard Java) provides a toDouble method that converts a string to a double value:. double fahrenheit

✓ If the Java file you specify on the command line contains a reference to another Java class that’s defined by a java file in the same folder, the Java compiler automatically