0% found this document useful (0 votes)
10 views

4.2-Day-Recape of Java Fundamentals

Fundamentals of java

Uploaded by

deepak879142
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

4.2-Day-Recape of Java Fundamentals

Fundamentals of java

Uploaded by

deepak879142
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Introduction

JAVA
—-----
1. Core Java
2. Adv. Java
3. Mobile Java

[Wrong Classification.]

JAVA
—------
1. J2SE / JAVA SE
2. J2EE / JAVA EE
3. J2ME / JAVA ME

J2SE / JAVA SE
—-------------------
1. JAVA 2 Standard Edition / JAVA Standard Edition
2. It will provide only the “Fundamentals of Java programming”.
3. It can be used to prepare “Standalone Applications”.
4. If we design/develop and execute any application without using
Client-Server Architecture or without distributing application logic over
multiple machines then that applications are called “Standalone
Applications”.

5. EX: Calculator, Calendar, Paint,.....

1 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

Stand Alone Application

1. Only 5% of the applications are Standalone Applications.


2. Its direct utilization is 5% only, but its dependency level is 1000%.
3. It is a dependent programming language for the technologies and
tools like below.

a. J2EE

b. Selenium Testing

c. Salesforce

d. Android

e. SAP Tools

f. Hadoop

—----
—----

J2EE / JAVA EE:

2 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

—--------------------
1. JAVA 2 Enterprise Edition / JAVA Enterprise Edition.
2. It will cover “Server Side Programming”.
3. It can be used to prepare “Enterprise Applications” or “Distributed
Applications”.

4. If we design and execute any application on the basis of Client-


Server architecture or by distributing application logic over multiple
machines then that applications are called “Distributed Applications”
or “Enterprise Applications”.

5. 95% of the applications are Enterprise Applications

6. EX:
a. All Banking Applications are Enterprise Applications.
b. All Healthcare Applications are Enterprise Applications.
c. All E-commerce Applications are Enterprise Applications.
d. All insurance Applications are Enterprise Applications.
e. All Social Network applications are Enterprise Applications.
f. All food delivery applications are Enterprise Applications.
—---
—---

J2ME/JAVA ME

3 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

—-------------------

1. JAVA 2 Micro Edition or JAVA Micro Edition.


2. It will provide Micro programming.
3. It can be used to prepare Mobile Based Applications.
4. If we design and execute any application on a mobile H/W system
then that application is called “Mobile Based Applications”.
5. 100% of the applications are mobile applications.
EX:
iMobile App from ICICI bank.

MyIdea App from Idea.

Bookmyshow

Swiggy or zomato

Whatsapp, Telegram

Phonepe, GPay
Note:
Mobile Applications are having very good demand , but J2ME
does not have demand due to the number of alternatives in the
form of Android, Apple-IPhone,......

Steps To prepare First Java Application:


—-----------------------------------------------------
1. Download and Install Java Software.
2. Download and Install java Editor.
3. Open Java File and Write Java program.
4. Save Java file in our computer.
5. Compile Java File.
6. Execute Java program

Download And Install Java Software:


-—----------------------------------------------

4 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

For that go on Google and type

[Download java 6 / Download java 7 / Download java 8]

For that create one oracle account after creating it then u can download
the software.

After installation of the Java software, to use java commands in command


prompt we have to set the “path” environment variable.

There are two ways to set the “path” environment.

1. Temporary Setup

2. Permanent Setup

Temporary Setup:

Use the following command on command prompt to set the “path”


environment variable temporarily.

set path=C:\Java\jdk1.8.0_202\bin;

If we set the “path” environment variable temporarily then it is valid up to


the present command prompt, not valid to the other command prompt.

If we want to make available the “path” environment variable to all the


command prompts then we have to set the path environment Variable
permanently.

Permanent Setup
—---------------------

5 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

1. Right Click on “This PC” on Desktop.


2. Select “properties”.
3. Click on “Advanced System Settings”.
4. Select “Advanced” [By Default Selected].
5. Click on the “Environment Variables…” button.
6. Go to “User Variables “ Part.
7. If we already have a “path” environment variable, select it and click
on the Edit button and add a new value[C:\Java\jdk1.8.0_202\bin] to
the existing “path” environment variable.
8. If no “path” variable exists in the user variables part then click on
“New” button and provide the following details.
a. Variable Name : path
b. Variable Value : C:\Java\jdk1.8.0_202\bin
9. Click on the “Ok” button.
10. Click on the “OK” button.

Que)
is it possible to install more than one Java software in a single
computer?
—--------------------------------------------------------------------------------------
Ans:
—---
Yes, it is possible to install more than one java software in a single
computer but with the following conditions.

1. All the Java softwares must be of the current operating system.


2. All the java softwares of different versions[JAVA6, JAVA7,JAVA8], at
least the difference must have existed in the sub versions.

Que) If we install multiple Java versions in a single computer then


which version will come in the command prompt to use?
—-------------------------------------------------------------------------------------------------
Ans:
—----
If we install multiple java versions in a single computer then which java
version we set in the “path” environment variable that java version will
come to the command prompt to use.

6 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

On command prompt:

C:\Users\Administrator>set path=C:\Java\jdk1.6.0_45\bin;
C:\Users\Administrator>javac -version

javac 1.6.0_45

C:\Users\Administrator>set path=C:\Java\jdk1.7.0_80\bin;
C:\Users\Administrator>javac -version

javac 1.7.0_80

C:\Users\Administrator>set path=C:\Java\jdk1.8.0_202\bin;
C:\Users\Administrator>javac -version

javac 1.8.0_202

Que)If we install multiple java versions in a single computer and if we


set all the multiple versions to the single “path” environment variable
then which version of java will come to the command prompt to use?
—---------------------------------------------------------------------------------------------
Ans:
—---
If we set multiple versions of java softwares to the single “path”
environment variable then the first version in the order what we set to the
“path” environment variable that java version will come to use in the
command prompt.

On Command Prompt:

C:\Users\Administrator>set path=C:\Java\jdk1.6.0_45\bin;C:\Java\
jdk1.7.0_80\bin;C:\Java\jdk1.8.0_202\bin;

C:\Users\Administrator>javac -version
javac 1.6.0_45

7 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

C:\Users\Administrator>set path=C:\Java\jdk1.7.0_80\bin;C:\Java\
jdk1.8.0_202\bin;C:\Java\jdk1.6.0_45\bin;

C:\Users\Administrator>javac -version
javac 1.7.0_80

C:\Users\Administrator>set path=C:\Java\jdk1.8.0_202\bin;C:\Java\
jdk1.6.0_45\bin;C:\Java\jdk1.7.0_80\bin;

C:\Users\Administrator>javac -version
javac 1.8.0_202

C:\Users\Administrator>

Que)
If we set “JAVA 1.8” version to the “path” environment variable
permanently and if we set “JAVA 1.6” version to the “path”
environment variable temporarily then which Java version will come
to us in the command prompt?
—---------------------------------------------------------------------------------------------
Ans:
—---
In this context, the Operating System will search for the java version first in
temporary setup , if no java set up exists temporarily then Operating
System will search for java set up in Permanent environment variables set
up.

In the above context, we are able to get a “temporary set up” provided java
version that is JAVA 1.6 version.

C:\Users\Administrator>javac -version

javac 1.8.0_202

8 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

C:\Users\Administrator>set path=C:\Java\jdk1.6.0_45\bin;

C:\Users\Administrator>javac -version
javac 1.6.0_45

C:\Users\Administrator>

As per the requirement, if we want to change java software from one


version to another version repeatedly then every time we have to set the
“path” environment variable continuously, this approach will increase the
burden to the developers, to simplify this process we have to use “batch
files”.

-->Prepare a separate batch file for each and every java version.

Java6.bat, java7.bat, java8.bat

--> In each and every batch provides the respective “path” environment
variable setup.

java6.bat
—----------
set path=C:\Java\jdk1.6.0_45\bin;

java7.bat
—----------
set path=C:\Java\jdk1.7.0_80\bin;

java8.bat
—----------
set path=C:\Java\jdk1.8.0_202\bin;

9 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

3. Execute the batch files as per the requirement.

On command prompt:
D:\java430>java6
D:\java430>set path=C:\Java\jdk1.6.0_45\bin;

D:\java430>java7
D:\java430>set path=C:\Java\jdk1.7.0_80\bin;

D:\java430>java8
D:\java430>set path=C:\Java\jdk1.8.0_202\bin;

Download And Install Java Editor:


—------------------------------------------
Editor: It is a Software, it will provide a very good environment to write java
programs and to save java programs.

EX: Notepad, Editplus, Notepad++, VS Code, Sublime Text, ……….

Note:
Editors are suggestible up to the learning process, not suggestible in real
time application development, In Real time application development
IDEs[Integrated Development Environment] are suggestible.

EX:
Eclipse, IntelliJ Idea, Netbeans,.....

Installation of Sublime Text Editor:

For that just type [Download sublime]

In google and click on the following link

https://www.sublimetext.com/download
10 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

And Install it.

To write a very simple java program we have to use the following elements.

1. A Java class
2. main() method in the Java class.
3. System.out.println(---) to display data on command prompt.

F:\java8To9\Test.java
—------------------------

class Test
{
public static void main(String[] args)
{
System.out.println("Welcome To CC Team!");
}
}

Save Java File:


—------------------
To save Java files we have to use the following rules and regulations.

11 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

1. If the Java file contains any public class, public abstract class, public
interface, public enum then it is mandatory to save that java file with
the public element [class, abstract class, interface, enum] name
only. If we violate this rule then compiler will raise an error

2. If no public element[class, abstract class, interface, enum] is existed


in the present java file then it is possible save that java file with any
name like abc.java or xyz.java, here java has given a suggestion like
to save java file with Main Class[A class which has main() method]
name instead of any other name.

EX1: FileName: abc.java

class Test
{
public static void main(String[] args){
System.out.println("Welcome To Java Programming!");
}
}

F:\java8To9>javac abc.java
F:\java8To9>java Test

Welcome To Java Programming!

EX2: Test.java

class Test
{
public static void main(String[] args){
System.out.println("Welcome To Java Programming!");
}
}

D:\java8To9>javac Test.java
D:\java8To9>java Test
Welcome To Java Programming!

12 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

EX3: Test.java

public class A
{
}
class Test
{
public static void main(String[] args){
System.out.println("Welcome To Java Programming!");
}
}

D:\java430>javac Test.java
Test.java:1: error: class A is public, should be declared in a file named
A.java

public class A{
^
1 error

EX4: A.java
public class A{
}
class Test{
public static void main(String[] args){
System.out.println("Welcome To Java Programming!");
}
}

D:\java9To10>javac A.java

D:\java9To10>java Test

Welcome To Java Programming!

Que)
Is it possible to provide more than one public class in a single java file?

13 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

—----------------------------------------------------------------------------------------------
Ans:
—----
No, it is not possible to provide more than one public class in a single java
file , because as per the java rules and regulations if we provide more than
one public class in a single java file then we must save that java file with
more than one name, it is not possible in all the Operating Systems.

EX5: A.java

public class A
{
}
public class B
{
}
class Test{
public static void main(String[] args){
System.out.println("Welcome To Java Programming!");
}
}

D:\java430>javac A.java
A.java:3: error: class B is public, should be declared in a file named B.java
public class B{
^
1 error

EX6: B.java
public class A{
}
public class B{
}
class Test{
public static void main(String[] args){
System.out.println("Welcome To Java Programming!");
}
}
14 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

D:\javaDemos>javac B.java
B.java:1: error: class A is public, should be declared in a file named A.java
public class A{
^
1 error

EX7: AB.java

public class A{
}
public class B{
}
class Test{
public static void main(String[] args){
System.out.println("Welcome To Java Programming!");
}
}

D:\java430>javac AB.java
AB.java:1: error: class A is public, should be declared in a file named
A.java
public class A{
^
AB.java:3: error: class B is public, should be declared in a file named
B.java
public class B{
^
2 errors

Compile Java File:


—------------------------
Que) What is the requirement to compile Java files?
—--------------------------------------------------------------
Ans:
—---
The main purpose of compiling java file is

15 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

1. To translate a java program from Source code to the bytecode.


2. To check developers mistakes in the program

To perform compilation, JAVA has provided a predefined command that is


‘javac’.

Syntax:
javac FileName.java

EX:
D:\javaDemos>javac Test.java

When we use the above command on command prompt, the Operating


System will perform the following actions.

1. Operating System will take “javac” command from Command prompt and
Operating System will search for javac command in its internal
commands list and at the locations referred by “path” environment
variable.

2. If the “javac” command does not exist at the above locations then the
Operating system will provide the following message on command
prompt.

'javac' is not recognized as an internal or external command,


operable program or batch file.
16 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

3. In the above context, we have to specify javac location to the operating


system, for this, we have to set the “path” environment variable to the
location where javac is available, that is “C:\Java\jdk1.8.0\bin”.
Ex:

E:\java8To9>set path=C:\Java\jdk1.8.0\bin;

4. If the Operating System identifies javac command on either of the


above locations then Operating System will execute javac command,
with this, Java Compiler software will be activated and Java compiler
software will perform the following actions.

Ex:

a. Compiler will take the provided java file name from command prompt.
b. Compiler will search for the provided file name at the current location.
c. If the provided java file name does not exist at the current location
then the compiler will provide the following error.

javac: file not found: Test.java

d. If the provided java file is identified at the current location then the
compiler will perform compilation right from the starting point of the file to
the ending point of the file.

e.In the java file, if any error is identified then the compiler will provide the
respective error messages on command prompt.

f.If no error is identified in the present java file then the compiler will finish
its compilation and the compiler will generate .class files.

Note:
Generating number of .class files is not on the basis of the number of java
files which we compiled, Generating number of .class files is completely
depending on the number of classes, abstract classes, interfaces, enums
and inner classes which we have used in the present java file.
17 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

EX: D:\JavaDemos\Test.java
—-----------------------------------
enum E{// E.class
}
interface I{//I.class
}
abstract class A{//A.class
}
class B{//B.class
class C{// B$C.class
}
}
class Test{// Test.class
public static void main(String[] args)
{
System.out.println("First Java Application");
}
}

D:\java430>javac Test.java

D:\java430>dir

Volume in drive D is Data


Volume Serial Number is 68E9-29EB

Directory of D:\java430
10-02-2023 17:18 179 A.class
10-02-2023 17:18 270 B$C.class
10-02-2023 17:18 223 B.class
10-02-2023 17:18 611 E.class
10-02-2023 17:18 86 I.class
10-02-2023 17:18 424 Test.class
10-02-2023 17:18 190 Test.java
D:\java430>

18 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

If We want to compile java file from current location [F:\Java8To9] and if we


want to send the generated .class files to a particular target location [D:\
KabhiKabhi]

then we have to use the ‘-d’ option along with the ‘javac’ command.

Syntax:
javac -d [targetLocation] FileName.java

EX: D:\java430\Test.java

—-----------------------------

enum E{// E.class


}
interface I{//I.class
}
abstract class A{//A.class
}
class B{//B.class
class C{// B$C.class
}
}
class Test{// Test.class
public static void main(String[] args)
{
System.out.println("First Java Application");
}
}

D:\java430>javac -d D:\KabhiKabhi Test.java


D:\java430>d:
D:\>cd KabhiKabhi
D:\KabhiKabhi>dir
Volume in drive E is New Volume
Volume Serial Number is 7E96-6D63

19 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

Note:
When you are specify the target location[Target Folder in a Specific
Drive] In that case u must note one thing which is Drive must be existed
in ur System but folder can be existed or non existed. Both case is
accepted.

Case-2:
In the java file , if we provide a package statement then we have to compile
the java file with -d option in order to generate folder structure w.r.t the
package name.

EX: F:\java8To9\Test.java
—------------------------------
package com.ccteam.core;
class Test{
public static void main(String[] args)
{
System.out.println("First Java Application");
}
}

D:\Java8To9> javac -d . Test.java


D:\java430>javac -d E:\abc Test.java

D:\java430>e:

E:\abc>cd com

E:\abc\com>cd ccteam

E:\abc\com\ccteam>cd core

E:\abc\com\ccteam\core>dir
Volume in drive E is New Volume
Volume Serial Number is 7E96-6D63

Directory of D:\abc\com\ccteam\core
20 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

10-02-2023 17:35 <DIR> .


10-02-2023 17:35 <DIR> ..
10-02-2023 17:35 198 A.class
10-02-2023 17:35 443 Test.class
6 File(s) 2,059 bytes
2 Dir(s) 295,629,217,792 bytes free

E:\abc\com\ccteam\core>

Que)
Is it possible to compile more than one java file by using a single javac
command?
—-------------------------------------------------------------------------------------------
Ans:
—---
Yes, it is possible to compile more than one java file by using a single javac
command with the following cases.

Case#1: javac File1.java File2.java File3.java ………


It is able to compile all the java files which we provided along with the javac
command.

Case#2: javac *.java


It is able to compile all java files which exist at the current location.

Case#3: javac Employee*.java


It is able to compile all java files which are started with Employee and end
with anything.

Case#4: javac *Address.java


It is able to compile all java files which are started with anything but ended
with Address.

Case#5: javac *Account*.java


It is able to compile all java files which includes Account.

Execute Java Application:


21 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

—---------------------------------
To execute a Java program, first open the command prompt and go to the
location where the Main class .class file exists, there use the following
command.

java MainClassName

F:\java8To9>java Test

Que)
What is the difference between “path” environment variable and
“classpath” environment variable?
—---------------------------------------------------------------------------------------------
Ans:
—---
“path” environment variable is able to provide the location information to the
Operating System where all the java commands exist.

set path=C:\Java\jdk1.8.0_202\bin;

“classpath” environment variable is able to provide the location information


to the compiler and JVM where the required .class files exist.
22 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

set classpath=E:\abc;

—-------------------------------------------------------------------------------------------------

Language Fundamentals:
—--------------------------------
To prepare Java applications we need some building blocks which must be
provided by the programming languages called “Language Fundamentals”.

Java has provided the following building blocks to prepare java


applications.

1. Tokens
2. Data Types
3. Type casting
4. Java Statements
5. Arrays

1. Tokens:
It is basically a sequence of characters that are treated as a unit as it cannot be further
broken down.
In programming languages like Java language- keywords (int, char, float, etc.)
identifiers (user-defined names), operators (+, -, *, /), delimiters/punctuators like
comma (,), semicolon(;), braces ({ }), etc. , strings can be considered as tokens.

Example 1:
int a=10; //Input Source code

Tokens
int (keyword), a(identifier), =(operator), 10(constant/value) and ;
(punctuation-semicolon)

23 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

The logical smallest individual unit in a program is called “Lexeme”.


The collection of lexemes that come under a particular group is called
“Tokens”.

It[Lexeme] is a sequence of characters in the source code that are matched by given
predefined language rules for every lexeme to be specified as a valid token.

EX:
int a = b + c * d;

Lexemes: int, a, =, b, +, c, *, d, ; —-----> 9 Lexemes

Tokens:
int —-----------> Data Types
a,b,c,d —-----> Variables
=, +, * —------> Operators
; —-------------> Special Symbol or Terminator/Delimeter

Total 4 types of Tokens.

To prepare Java applications/Programs, JAVA has provided the following


tokens.

1. Identifiers
2. Literals
3. Keywords / Reserved Words
4. Operators

1. Identifiers:
—----------------
Identifier is a name assigned to the programming elements like variable,
method, classes, abstract classes, interfaces,.......

24 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

To provide identifiers in java applications, we have to use the following


rules and regulations.

1. Every java identifier should not start with a number, they may start
with an alphabet or _ symbol or $ symbol, but the subsequent
symbols may be a number or an alphabet or _ symbol or $ symbol.

EX:
int eno = 111; —---------------------------------> Valid
String 9eid= “RSS-999”; —--------------------> Invalid
float _esal = 50000.0f; —----------------------> Valid
float $esal = 50000.0f; —----------------------> Valid
String emp_Addr = “AGRA”; —------------------> Valid
float esalIn$ = 50000.0f; —--------------------> Valid
String emp9_Email = “[email protected]”; —--> Valid

2. Every Java identifier should not allow spaces in the middle.

EX:
int empNo = 111; —--------------> Valid
int emp No = 111; —-------------> Invalid
String empAddr = “AGR”; —----> Valid
String emp Addr = “AGR”; —---> Invalid

3. Every Java identifier should not allow operators:

EX:
String empName = “CCTeam”; —---------------> Valid
String emp+Addr = “AGR”; —------------------> Invalid
String emp-Email = “[email protected]”; —----> Invalid
String emp*Type = “Permanent”; —-----------> Invalid

25 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

4. Every Java identifier should not allow special symbols except _


symbol and $ symbol:

EX:
int emp.No = 111; —-----------------------------> Invalid
String emp,Name = “CCTeam”; —---------------> Invalid
String emp-Addr = “AGR”; —--------------------> Invalid
String emp_Email = “[email protected]”; —--> Valid
String emp@AGR = “CCTeam”; —----------------> Invalid

5. Every Java identifier should not allow primitive data types and
keywords.

EX:
int a = 10; —-----------------> Valid
int break = 20; —-----------> Invalid
String str = “abc”; —-------> valid
String int = “abc”; —-------> Invalid
float for=22.22f; —-------> Invalid

6. In Java applications, it is possible to use all predefined classes


names as identifiers.

EX:
class Test{
public static void main(String[] args){
int a = 10;
System.out.println(a);

int Exception = 10;


System.out.println(Exception);
}
}

26 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

Status: No Compilation Error

OP:
10
10

EX:
class Test{
public static void main(String[] args){
String str = "String";
System.out.println(str);

String String = "String";


System.out.println(String);
}
}

Status: No Compilation Error


OP:
String
String

EX:
class Test
{
public static void main(String[] args){
int System = 10; //Ok
System.out.println(System);
}
}

Status: Compilation Error


Reason:
If we declare any predefined class name as an integer variable then
in the remaining part of the program we must use that class name as
integer variable only, not possible to use like its original class name. If
27 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

we use that predefined class name as like the original class name
then the compiler will raise an error.

In the above context, if we want to use that predefined class name as


like its original class name then we have to use
“Fully Qualified Name”.

class Test{
public static void main(String[] args){
int System = 10;
java.lang.System.out.println(System);// 10
System = System + 10;
java.lang.System.out.println(System);// 20
System = System + 10;
java.lang.System.out.println(System);// 30
}
}

Along with the above rules and regulations, JAVA has provided the
following suggestions to provide identifiers in java.

1. In Java applications, it is suggestible to provide all identifiers with a


particular meaning.

EX:
String xxx = “abc123”; —----> Not Suggestible
String accNo = “abc123”;----> Suggestible

2. In Java applications, there is no length restriction for the identifiers,


we can write identifiers with any length , but it is suggestible to
manage the length of the identifiers up to around 10 symbols.

EX:
String temporaryemployeeaddress = “AGRA”; —--> Not Suggestible
28 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

String tempEmpAddr = “Mathura”; —-----> Suggestible

3. In Java applications, if we have multiple words in a single identifier


then it is suggestible to separate those multiple words with the special
symbols like ‘_’ symbol.
EX:
String tempEmpAddr = “AGR”; —------> Not Suggestible
String temp_Emp_Addr = “AGR”; —---> Suggestible
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Literals:
—--------
Literal is a constant assigned to the variables.
EX:
int a = 10;

int —---> Data Type


a —----> variable.
= —----> operator
10 —---> Constant [Literal]
; —------> Special symbol /Terminator/Delimiter

To prepare java applications, JAVA has provided the following list of


literals.

1. Integral Literals / Integer Literals:


byte, short, int, long —---> 10,20, 30,.....
char —------> ‘a’, ‘b’, ‘\t’, ‘\n’,.....

Note:
To represent Long values we have to provide a number with
the suffix either ‘l’ or ‘L’.

EX:

29 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

class Test{
public static void main(String[] args){
long mobile1 = 9760433226l;
System.out.println(mobile1);

long mobile2 = 9760014754L;


System.out.println(mobile2);
}
}

2. Floating point Literals:


float —---> 22.22f, 345.344F,......
double —--> 345.456, 567.345d, 7786.23454D

Note:
To represent float values we have to use either ‘f’ or ‘F’ as
Suffix.

Note:
To represent Double values we have to use either no suffix or
‘d’ or ‘D’.

EX:
class Test{
public static void main(String[] args){
float temp1 = 32.5f;
float temp2 = 98.8F;
System.out.println(temp1);
System.out.println(temp2);
}
}

EX:
class Test{

30 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

public static void main(String[] args){


double balance1 = 123456.3456;
double balance2 = 5678.345d;
double balance3 = 987654.3456D;

System.out.println(balance1);
System.out.println(balance2);
System.out.println(balance3);
}
}

3. Boolean Literals:
boolean —---> true, false
EX:
class Test{
public static void main(String[] args){

boolean b1 = true;
boolean b2 = false;
System.out.println("To day is Tuesday : "+b1);
System.out.println("To day is Sunday : "+b2);
}
}

4. String Literals:
If we provide any data including alphabets, digits, special symbols,....
By enclosing it with “ “ then that data is String literal.
EX:

class Test{
public static void main(String[] args){

String dateOfBirth = "18-08-1981";


String mobileNumber = "91-9760014754";
String emailId = "[email protected]";

31 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

System.out.println("Date Of Birth : "+dateOfBirth);


System.out.println("Mobile Number : "+mobileNumber);
System.out.println("Email Id : "+emailId);
}
}

Note:
In Java applications, only String data type is able to represent any
type of data.

To improve Readability in literals , JAVA 7 version has provided a flexibility


for us to include ‘_’ symbols in the literals.

EX:
double d = 12_34_56_789.34567;

If we compile the above code, Compiler will remove the provided ‘_’
symbols from the number and compiler, JVM will process that number as
like the original number.

EX:
class Test{
public static void main(String[] args){
double d = 12_34_56_789.34567;
System.out.println(d);
}
}

Number Systems in Java:


—--------------------------------
In the programming languages to represent numbers we have to follow
some standards called “Number Systems”.

There are four types of Number Systems.

32 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

1. Binary Number System


2. Octal Number System
3. Decimal Number System
4. Hexa-Decimal number System

All the above number systems are supported in JAVA , but the default
number system in java is “Decimal number System”.

Binary Number System:


Base Value: 2
Input Alphabet: 0,1
Prefix : 0b or 0B

EX:
int a = 10; ----------> It is a decimal number, not a binary number.
int b = 0b1010;-------> Valid
int c = 0B1100;-------> Valid
int d = 0b1020; —---> Invalid

EX:
—--
class Test
{
public static void main(String[] args){
int a = 0b10; // 0*2p0 + 1*2p1 ==> 0 + 2 ===> 2
System.out.println(a); // 2
int b = 0B1010; // 0*2p0+1*2p1+0*2p2+1*2p3 => 0+2+0+8=>10
System.out.println(b); // 10
}
}

Note:

33 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

Binary number system was introduced by JAVA 7 version, it is not


supported in java applications up to JAVA 6 version, it is supported right
from JAVA 7 version.

Octal Number System:


Base Value : 8
Input Alphabet: 0,1,2,3,4,5,6,7
Prefix : 0 [zero]

EX:
int a = 10; —-----> It is a decimal number, not octal number.
int b = 01234; —-> Valid
int c = o4567; —--> Invalid
int d = 05678; —--> Invalid

EX:
class Test
{
public static void main(String[] args){
int a = 010; // 0*8p0 + 1*8p1 ==> 0 + 8 ==> 8
System.out.println(a);// 8

int b = 0123;// 3*8p0 + 2*8p1 + 1*8p2 ==> 3+16+64=> 83


System.out.println(b);// 83
}
}

Decimal Number System:


Base Value : 10
Input Alphabet: 0,1,2,3,4,5,6,7,8,9
Prefix: No prefix

Hexa-Decimal Number System:


Base Value : 16
Input Alphabet: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f
Prefix : 0x or 0X

34 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

EX:
int a = 10; -------> It is a decimal number, not an hexa-decimal number
int b = 0x123456; —--> Valid
int c = 0X6789; —-----> Valid
int d = 0x9abcd; —----> Valid
int e = 0Xdefg; —------> Invalid

EX:
class Test
{
public static void main(String[] args){

int a=0x10; // 0*16p0+1*16p1 ==> 0+16 ==> 16


System.out.println(a);// 16

int b = 0X123;//3*16p0+2*16p1+1*16P2=>3+32+256=> 291


System.out.println(b);// 291

int c = 0xabcd;//13*16p0+12*16p1+11*16p2+10*16p3==>43981
System.out.println(c);// 43981
}
}

Note:
In Java applications, if we provide any number in any number system and if
we compile the java application then compiler will recognize the provided
number and its number system, Compiler will convert that number from the
specified number system to decimal number system, compile and JVM will
process that number as like a decimal number.

In Java applications, we can perform arithmetic operations over the


numbers which are in different systems.
EX:
class Test{
public static void main(String[] args)
{
35 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses
C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

int a = 0b1010 + 012;


System.out.println(a);

int b = 0123 + 0xabc;


System.out.println(b);
}
}

D:\java430>javac Test.java

D:\java430>java Test
20
2831

Que) Find the number system from the following numbers?


1. int a = 00; -—> Octal number
2. int b = 0; —---> Decimal Number System
3. int c = 0x123; —-> Hexa-decimal number system
4. Int d = 0b1011 —> Binary number system

Keywords / Reserved Words:


—-------------------------------------
If any predefined word has word recognition and internal functionality
then that predefined word is called “Keyword”.

Ex:
int var=10;

EX:
1. Data Types and Return Types:
byte, short, int, long, float, double,boolean, char, void

2. Access Modifiers:

36 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)
Introduction

public, protected, private, static, final, abstract, native, volatile,


transient, synchronized, strictfp,......

3. Flow Controllers:
if, switch, case, default, for, while, do, break, continue, return,........

4. Class/Object Related Keywords:


class, enum, interface, extends, implements, new, this, super,
package, import,......

5. Exception Handling Related Keywords:


throws, throw, try, catch, finally

Reserved word/Deprecated Word is a predefined word which has only word


recognition but no functionality.

EX: const, goto.

Note:
In Java applications, we are able to use Keywords , but we are unable to
use Reserved words, because Reserved words are not having internal
functionality.

37 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(17 Years Experience)

You might also like