4.2-Day-Recape of Java Fundamentals
4.2-Day-Recape of Java Fundamentals
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”.
a. J2EE
b. Selenium Testing
c. Salesforce
d. Android
e. SAP Tools
f. Hadoop
—----
—----
—--------------------
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”.
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
—-------------------
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,......
For that create one oracle account after creating it then u can download
the software.
1. Temporary Setup
2. Permanent Setup
Temporary Setup:
set path=C:\Java\jdk1.8.0_202\bin;
Permanent Setup
—---------------------
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.
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
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
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
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>
-->Prepare a separate batch file for each and every java version.
--> 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;
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;
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,.....
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
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!");
}
}
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
class Test
{
public static void main(String[] args){
System.out.println("Welcome To Java Programming!");
}
}
F:\java8To9>javac abc.java
F:\java8To9>java Test
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!
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
Que)
Is it possible to provide more than one public class in a single java file?
—----------------------------------------------------------------------------------------------
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
Syntax:
javac FileName.java
EX:
D:\javaDemos>javac Test.java
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.
E:\java8To9>set path=C:\Java\jdk1.8.0\bin;
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.
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
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>
then we have to use the ‘-d’ option along with the ‘javac’ command.
Syntax:
javac -d [targetLocation] FileName.java
EX: D:\java430\Test.java
—-----------------------------
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:\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
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.
—---------------------------------
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;
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”.
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)
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;
Tokens:
int —-----------> Data Types
a,b,c,d —-----> Variables
=, +, * —------> Operators
; —-------------> Special Symbol or Terminator/Delimeter
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,.......
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
EX:
int empNo = 111; —--------------> Valid
int emp No = 111; —-------------> Invalid
String empAddr = “AGR”; —----> Valid
String emp Addr = “AGR”; —---> Invalid
EX:
String empName = “CCTeam”; —---------------> Valid
String emp+Addr = “AGR”; —------------------> Invalid
String emp-Email = “[email protected]”; —----> Invalid
String emp*Type = “Permanent”; —-----------> Invalid
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
EX:
class Test{
public static void main(String[] args){
int a = 10;
System.out.println(a);
OP:
10
10
EX:
class Test{
public static void main(String[] args){
String str = "String";
System.out.println(str);
EX:
class Test
{
public static void main(String[] args){
int System = 10; //Ok
System.out.println(System);
}
}
we use that predefined class name as like the original class name
then the compiler will raise an error.
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.
EX:
String xxx = “abc123”; —----> Not Suggestible
String accNo = “abc123”;----> Suggestible
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
Literals:
—--------
Literal is a constant assigned to the variables.
EX:
int a = 10;
Note:
To represent Long values we have to provide a number with
the suffix either ‘l’ or ‘L’.
EX:
class Test{
public static void main(String[] args){
long mobile1 = 9760433226l;
System.out.println(mobile1);
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{
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){
Note:
In Java applications, only String data type is able to represent any
type of data.
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);
}
}
All the above number systems are supported in JAVA , but the default
number system in java is “Decimal number System”.
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:
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
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 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.
D:\java430>javac Test.java
D:\java430>java Test
20
2831
Ex:
int var=10;
EX:
1. Data Types and Return Types:
byte, short, int, long, float, double,boolean, char, void
2. Access Modifiers:
3. Flow Controllers:
if, switch, case, default, for, while, do, break, continue, return,........
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.