core java
Short Description
java basics...
Description
JAVA LEARNING CENTER 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
Programming Language
JAVA LEARNING CENTER 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 . Instructions written in High Level Language will not be understood by the machine directly .
JAVA LEARNING CENTER 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.
C Language Program Development and Exception Model
Working on Window Platform
Hello.c
C Source Code
C Compiler
Machine Code Or Native Code
Will be executed Successfully
WINDOWS
Will not be executed Successfully
MAC OS
Will not be executed Successfully
LINUX
JAVA LEARNING CENTER BY-A.K.JAISWAL
java Language Program Development and Exception Model
Working on Window Platform
Hello.java java Source Code
Java Compiler
Hello.class
JVM
Native Code
Will be executed Successfully
WINDOWS
1. What is Source Code
Byte Code
JVM
Native Code
Will be executed Successfully
MAC OS
JVM
Native Code
Will be executed Successfully
LINUX
JAVA LEARNING CENTER 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? JIT(just –in –time)compiler is a component of the java Runtime Environment.
JAVA LEARNING CENTER 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
Development Tools
Hello.java
Javac
java
javap
jar
Java Runtime Environment/java Virtual Machine
etc
JAVA LEARNING CENTER BY-A.K.JAISWAL
Byte Code Verifier Security manager
Hello.class
Class Loader
Class Files
Method
Heap
Area
Memory
Execution Engine
Stack
PC
Memory
Register
JIT
GC
Compiler 0101010
0101001
Operation System/Platform
Hardware
Other Components
JAVA LEARNING CENTER 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. Standerd Edition(SE)
Enterprise Edition(EE)
Micro Edition(ME)
Java Standard Edition Versions Java Version JDK Alpha and Beta JDK 1.0 JDK 1.1 J2SE 1.2 J2SE 1.3 J2SE 1.4
Code Name Oak Playground Kestrel Merlin
Released Year 1995 1996 1997 1998 2000 2002
JAVA LEARNING CENTER BY-A.K.JAISWAL
J2SE 5.0 J2SE 6 JAVA SE 7 JAVA SE 8
Tiger Mustang Dolphin
2004 2006 2011 18 march 2014
Java Enterprise Edition Version EE version Java(java Professional)
Released Year 1998
J2EE1.3
2001
J2EE1.4
2003
JEE 5
2006
JEE 6
2009
JEE 7
2013
Technologies Added JDBC 2.0 JNDI 1.2 Servlets 2.2 Jsp1.1 EJB 1.1 Java Mail API1.1 Servlets 2.3 JSP1.2 EJB2.0 Java Mail API 1.2 Servlets 2.4 JSP 2.0 EJB 2.1 Java Mail API1.3 Servlets 2.5 JSP 2.1 JSF 1.2 EJB3.0 JPA1.0 Java Mail API1.4 JDBC 4.0 Servlets 3.0 JSP 2.2 JSF 2.0 EJB 3.1 JPA2.0 JDBC 4.1 Servlets 3.1 JSP 2.3 JSF 2.2 EJB 3.2 JPA2.1
JAVA LEARNING CENTER 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.
JRE Derectory
JDK Directory
C:
E:
Program Files
Jdk 1.7.0 bin Javac Java.exe
Javap
lib ,, jar.exe ,,etc
jre
Srz.zip Private Jre
java
Jre7 Public jre
JAVA LEARNING CENTER 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 JDK Directory
Workspace D:
E: Jdk 1.7.0 bin
Java.exe,java.exe
Mukesh Ankush Hello.java
Java.exe,javap.exe Jar.exe,,etc
1. Open Command Prompt as Follows • • •
Click on Start -> Run Type cmd Click on OK
JAVA LEARNING CENTER 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 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
• • • • •
•
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 Click on button in all the windows.
JAVA LEARNING CENTER 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 Java doesn’t support pointer Java class not support multiple inheritance with classes. Java doesn’t support Global Variable (variables declared outside the class) Java doesn’t support header files.
C++ C++ support pointer C++ supports multiple inheritance with classes. C++ supports Global Variables.
C++ support header files.
JAVA LEARNING CENTER BY-A.K.JAISWAL
Const and goto keywords can’t used in java . Java doesn’t supports destructor Java doesn’t support virtual function Java doesn’t support operator overloading . Java doesn’t support scope Resolution Operator. Java has a garbage collector to clean the memory automatically. Java has built- in API for multithreading and network Programming
Const and goto keywords can be used in c++. C++ support Destructor C++ support virtual function. C++ support operator Overloading. C++ support scope resolution operator. No automatic memory cleaning process in c++. C++ doesn’t have Built- in API for multithreading and network Programming
JAVA LEARNING CENTER BY-A.K.JAISWAL
Java Language Ref Guide Part 1
JAVA LEARNING CENTER 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 Digits Special Characters White Space character
A-Z/a-z. 0-9 +-*&! @#$%^ etc newline (\n), carriage return (\r)
etc
2. Keywords/Reserved words • Keywords are simple English 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.
true, false, null are not keywords but reserved as Literals.
JAVA LEARNING CENTER 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) Class & object(9)
Access Modifier(3) Modifier(9)
Package(2) Control Statements(12)
Exception Handling(6) Other data Type(1)
boolean ,byte ,chat ,short, int ,float, double class , interface , enum, extends, implements , this , supper, new. Private, protected ,public final , native, abstract, synchronized ,transient, volatile ,static ,const* package, import if ,else ,switch, case, defaults ,do ,while, break, for, continue, return try , catch, finally, throw, throws 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. • Identifiers are also known as User Defined Words.
JAVA LEARNING CENTER 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 Int getClassName Total_FFE
1stClass true student number 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. boolean
byte
char
short
int
long
float
double
JAVA LEARNING CENTER BY-A.K.JAISWAL
boolean
Boolean Types
byte
short Integral Types
Primitive Types
int
long
Numeric Types
Floating Point Types
char
float double
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)
JAVA LEARNING CENTER BY-A.K.JAISWAL
Type
Size Byte N/D
Default value
Min value
Max value
True or -27(128) 0
false 27-1(127) 216-1 (65535)
-215 (32,768) -231 (2147483648) -263 1.4E-45 4.9E-324 Reference of
215-1 (32,767)
boolean Byte Char
1 2
Bits N/D 8 16
short
2
16
false 0 ASCII-0 Unicode\u0000 0
Int
4
32
0
Long Float Double Any reference type
8 4 8 8
64 32 64 64
0 0.0 0.0 Null
231-1 (2147483647) 263-1 3.40E38 1.79E308 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 ; =;
EX Int a;
String str; str
a
0
4 bytes
null
8 bytes
JAVA LEARNING CENTER BY-A.K.JAISWAL
Int a=99; a
String str =”SRI”; str
99
4 bytes
32597
SRI
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” ;
JAVA LEARNING CENTER BY-A.K.JAISWAL
Primitive Variable
Reference Variable
Variable declared with primitive data types are called as primitive variable Memory allocation for primitive variables depends on the primitive data types Default value for primitive variables depends on primitive data types Primitive variables hold valid literals or value in the allocated memory.
Variable declared with reference data types are called as reference variables. Always 8 byte of memory will be allocated for reference variables. Always null will assigned as default value for reference variables. 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.
JAVA LEARNING CENTER BY-A.K.JAISWAL
Class Hello { Int a; Static int b;
instance variable static variable
void show(){ Int c;
local variable
} }
There are various syntax for declaring variables •
Declaring a single variable in one variable declaration statement.
Syntax [modifiers] ; EX byte b; int a; double d; char ch; String str; Hello h; Etc
JAVA LEARNING CENTER BY-A.K.JAISWAL
Initializing a single variable syntax; = ;
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],,,…..;
EX byte b1,b2,b3,b4; int a,b,c; char ch1,cj2,ch3; String str1.str2,str3;
Initializing multiple variables with same value
JAVA LEARNING CENTER BY-A.K.JAISWAL
Syntax ===; EX a=b=c=9898; str1=str1=str3=”Ankush” ;
Declaring and initializing multiple variable of single type in one variable declaration statement. Syntax [modifiers] =,=,=…….;
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 = ;
EX Final int a=99;
JAVA LEARNING CENTER 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 ;
= ; 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.
SUMMARY 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 .
JAVA LEARNING CENTER 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 • Assign value of variable a to variable a
JAVA LEARNING CENTER 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
Int Data type
String
Variable Name
a=
Literals
123;
Variable Name
str =
”Ankush” ;
Literals
JAVA LEARNING CENTER BY-A.K.JAISWAL
Literals
Non Numeric
Numeric
boolean
Signed
Unsigned
char
integral
Floating
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
String
JAVA LEARNING CENTER 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
Tab space \t \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 \uXXXX
-X will be hexadecimal digits
JAVA LEARNING CENTER 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 Range in octal
Char
ASCII 0 1 2 3 4 5 6 7 8 9 : ;
48 49 50 51 52 53 54 55 56 57 58 59
0
-
255
\0
-
\377
OCTAL 060 061 062 063 064 065 066 067 070 071 072 073
UNIC 30 31 32 33 34 35 36 37 38 39 3a 3b
UNICODE 30 means \u0030 and so on
Char
ASCII < = > ? @ A B C D E F G
60 61 62 63 64 65 66 67 68 69 70 71
OCTAL 074 075 076 077 100 101 102 103 104 105 106 107
UNIC 3c 3d 3e 3f 40 41 42 43 44 45 46 47
JAVA LEARNING CENTER 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
JAVA LEARNING CENTER 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
12345
JAVA LEARNING CENTER 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. • You must use F/f as suffix for float value
JAVA LEARNING CENTER 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 Tens Units
1/10 1/100 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.
JAVA LEARNING CENTER 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 2,45,12,452
JAVA LEARNING CENTER BY-A.K.JAISWAL
you can do the same task with UNDERSCORE (_) from java 7 towards EX 2_45_12_452
SUMMARY
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
4)
5) 6) 7) 8)
true not same as True TRUE, True, TrUe etc false not same as FALSE ,False, FaLse etc a char type variable can hold following Single character enclosed in single quotation marks Escape sequence ASCII value UNICODE character SPACE is valid character so it can be placed in single quotation marks TAB is valid character so it can be placed in single quotation marks Back space character (\) is used to form escape sequence so only backspace character can’t be placed in single quotation marks Single quote is used to form character literal so only single quote can’t be placed in single quotation marks
JAVA LEARNING CENTER 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
JAVA LEARNING CENTER 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 33) Default type of Floating point Literal is doule
JAVA LEARNING CENTER 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
JAVA LEARNING CENTER 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 • Bitwise operators
JAVA LEARNING CENTER 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
10
+
operand2
20
Unary Arithmetic operators Operator +
Name
-
Minus
plus
description Return same value with same sign Return same value with opposite sign
Binary Arithmetic operators Operator + * / %
Description Addition(sum) Subtraction(difference) Multiplication(product) Division(quotient) Modulus(remainder)
JAVA LEARNING CENTER 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.
1st operand type
2nd operand
Result type
byte char short int int byte float float char int char short short etc
byte char byte int long double int long double double int char float
int int int int Long double float float double double int int float
String Concatenation Operator(+) • •
It is a binary operator + operator can be used for two purposes
Arithmetic addition String concatenation
JAVA LEARNING CENTER 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 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
short int char
long
float
double
JAVA LEARNING CENTER BY-A.K.JAISWAL
Type Casting • = • 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 = () 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
JAVA LEARNING CENTER 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; byte b=(byte)a;
//invalid implicit casting //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; 0 0 1 0 1 1 0 0 Value is
->byte means 8 byte memory required
JAVA LEARNING CENTER BY-A.K.JAISWAL 7
6
4
3
2
1
0*2 +0*2 +1*2 +0*2 +1*2 +0*2 +0*2
0
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
JAVA LEARNING CENTER BY-A.K.JAISWAL
^= = >>>=
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 > < >= b) && (a>c) (a>> 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 0
0
1
0
1
1
0
1
1
1
0
1
0
0
1
0
Actual Bits 1’s Complement
2’s Complement • 2’s complement of a binary number is formed by adding 1 to the least significant bit of 1’s complement
JAVA LEARNING CENTER BY-A.K.JAISWAL
0
0
1
0
1
1
0
1
1
1
0
1
0
0
1 + 1
0 1 1
1
1
0
1
0
0
Actual Bits 1’s Complement Adding 1 with value 2’s Complement
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
1
64 32 16 8 0 0 0 0 1
4 1
2 1
1 1
Binary of 15
0
0
0
0
1’s Complement (~x)=-16
64 32 16 8 0 0 1 0 1
4 1
2 0
1 1
Binary of 45
0
1
0
1
1
1
Int x =45; Sign bit
1 1 0 1 Shortcut method
0
~OPERAND = -(OPERAND + 1)
1’s Complement (~x) = -46
JAVA LEARNING CENTER 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 also will be evaluated When operands are Boolean type then result is boolean type When operands are integral type then result is integral type.
When first expression value is false then second expression will not be evaluated Result is Boolean type
OP1
OP2
Result (Op1 & Op2)
1 1 0 0
1 0 1 0
1 0 0 0
Bitwise OR Operator • Bitwise OR operator is similar to logical OR Operator with following defferences.
Logical OR Operands are Boolean type only
Bitwise OR Operands can be Boolean as well as integral
JAVA LEARNING CENTER BY-A.K.JAISWAL
type When first expression value is true then second expression also will be evaluated When operands are Boolean type then result is boolean type When operands are integral type then result is integral type.
When first expression value is true then second expression will not be evaluated Result is Boolean type
OP1
OP2
Result (Op1 & Op2)
1 1 0 0
1 0 1 0
1 0 1 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 0
1 0 1 0
0 1 1 0
Left shift Operator () 2. It is used to shift the bits towards right side by specified number of bits. Will be ignored
0
0
0
…
0
0
0 0
0
0
1
1
1
1
0
0
0
0
0
0
0
0
0
…
0 0
0
0
0
0
1
1
1
1
0
0
Will be filled with actual sign bit
Will be ignored
1
0
1
…
0
0
0 0
0
0
1
1
1
1
0
0
0
0
1
1
1
0
1
…
0 0
0
0
0
0
1
1
1
1
0
0
Will be filled with actual sign bit
Unsigned Right Shift (>>>) 3. It is same as Right Shift operator but result will be always positive
Will be ignored
0
0
0
0
0
0
…
0
0
0 0
0
0
1
1
1
1
0
0
0
0
0
0
…
0 0
0
0
0
0
1
1
1
1
0
0
Will be filled with 0 always
JAVA LEARNING CENTER BY-A.K.JAISWAL Will be ignored
1
0
1
…
0
0
0 0
0
0
1
1
1
1
0
0
0
0
0
0
1
0
1
…
0 0
0
0
0
0
1
1
1
1
0
0
Will be filled with 0 always
SUMMARY
1. Result of Arithmetic Expression is always int type or wider than int type 2. Result of Arithmetic Expression will not be byte , char or short type. 3. When you want to use two same unary Arithmetic Operators continuously then you must separate those two operators with parenthesis or space. 4. When you use two same unary Arithmetic Operators continuously without parenthesis or space then it will be considered as INCREMENT or DECREMENT Operator 5. When you want to use two different unary Arithmetic Operators continuously then parenthesis or space is not required 6. When char type operators is used in arithmetic expression then ASCII value will be used while performing the operation 7. Int value can be stored in byte , char or short type variable if it satisfies following rules It must be final It must be initialized in the same statement
JAVA LEARNING CENTER BY-A.K.JAISWAL
It must be in the range of corresponding data type 8. When the operands are int or lower than int type then result will be int type only 9. When any one operand or both the operands are wider than int type then result will be wider type. 10.Long value can be stored in float type variable because floating point data type stores the value in exponential representation. 11.Long or wider then long can’t be assigned to lower type variable even it is final 12.Following are some expressions having floating point operands which result infinity , -infinity or NaN (Not a Number). /0.0 infinity /0 infinity /-0.0 infinity /0.0 -infinity /0 -infinity
0.0/0.0 0/0.00 -0.0/0.0 0.0/0
NaN NaN NaN NaN
13. When you divide any integer value with 0 then it throws java.lang.ArithmeticException at runtime. 14.Infinity concept is not available with integral type because their range is finity. 15.Infinity concept is available with floating types because their range is near to infinity. 16.Operator Preceence 7+(6+5*3)-4/2
JAVA LEARNING CENTER BY-A.K.JAISWAL
7+(6+15)-4/2 7+21-4/2 7+21-2 28-2 26 17.Precedence of Arithmetic operators are as following. Unary +exp -exp Multiplicative */ % Additive + 18.When multiple operators of same precedence found in an expression then that will be evaluated from left to right 19.The variable , value or value of an expression must be assigned to compatible type.
byte
short int
long
float
double
char
20.Type casting is possible only with compatible data type. 21.Following are some cases where type casting is not possible boolean b= (Boolean)1; String str= (String)99; 22.In the case of explicit casting , Right side bits will be used as per the number of bits required for new data type . Remaining all bits will be discarded Int a=-1;
->int means 32 bits memory required
JAVA LEARNING CENTER BY-A.K.JAISWAL
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 char ch=(char)a;
->char means 16 bits memory required
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Value
->65535
Int x=ch; ->int means 32 bits memory required 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Value is => 65535. 23.Priority of explicit casting is higher than other operators. 24.
View more...
Comments