Core Java: Made Simple: A popular language for Android smart phone application, favoured for edge device and
()
About this ebook
Book will help you to understand each and every topic of java practically. It will help you in developing software and websites because one should have sound practical knowledge. It covers all the topics which are important from the point of view of the interview, certification and examinations and no topic is left untouched.
Related to Core Java
Related ebooks
Getting Skilled with Java: Learn Java Programming from Scratch with Realistic Applications and Problem Solving Programmes (English Edition) Rating: 0 out of 5 stars0 ratingsJava Multithreading Interview Questions And Answers Rating: 0 out of 5 stars0 ratings100+ Solutions in Java: A Hands-On Introduction to Programming in Java (English Edition) Rating: 0 out of 5 stars0 ratingsPractical Java 8: Lambdas, Streams and new resources Rating: 5 out of 5 stars5/5Java: A complete practical solution Rating: 0 out of 5 stars0 ratingsJAVA Programming Simplified: From Novice to Professional - Start at the Beginning and Learn the World of Java Rating: 0 out of 5 stars0 ratingsCode with Java 21: A practical approach for building robust and efficient applications (English Edition) Rating: 0 out of 5 stars0 ratingsPractical Java Programming for IoT, AI, and Blockchain Rating: 0 out of 5 stars0 ratingsJava Programming Rating: 0 out of 5 stars0 ratingsJava 8 to 21: Explore and work with the cutting-edge features of Java 21 (English Edition) Rating: 0 out of 5 stars0 ratingsJAVA: Java Programming for beginners teaching you basic to advanced JAVA programming skills! Rating: 0 out of 5 stars0 ratingsLearn Java 12 Programming: A step-by-step guide to learning essential concepts in Java SE 10, 11, and 12 Rating: 0 out of 5 stars0 ratingsJAVA 9.0 To 13.0 New Features: Learn, Implement and Migrate to New Version of Java. Rating: 0 out of 5 stars0 ratingsData Structures and Algorithms Implementation through C: Let’s Learn and Apply Rating: 0 out of 5 stars0 ratingsJava Programming: 24-Hour Trainer Rating: 0 out of 5 stars0 ratingsMastering Java: A Comprehensive Guide to Programming Excellence Category Rating: 0 out of 5 stars0 ratingsAndroid Studio Electric Eel Essentials - Kotlin Edition: Developing Android Apps Using Android Studio 2022.1.1 and Kotlin Rating: 0 out of 5 stars0 ratingsLearning Network Programming with Java Rating: 0 out of 5 stars0 ratingsOCA Java SE 8 Programmer I Certification Guide Rating: 0 out of 5 stars0 ratingsNode.JS Guidebook: Comprehensive guide to learn Node.js Rating: 0 out of 5 stars0 ratingsAdvanced JAVA Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsSpring Data Rating: 0 out of 5 stars0 ratingsCore Java Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 4 out of 5 stars4/5Mastering Concurrency Programming with Java 8 Rating: 0 out of 5 stars0 ratingsJava / J2EE Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratings
Programming For You
Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Spies, Lies, and Algorithms: The History and Future of American Intelligence Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Tiny Python Projects: Learn coding and testing with puzzles and games Rating: 4 out of 5 stars4/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsC# Programming from Zero to Proficiency (Beginner): C# from Zero to Proficiency, #2 Rating: 0 out of 5 stars0 ratingsHTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5C# 7.0 All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsiPhone For Dummies Rating: 0 out of 5 stars0 ratingsJavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5C All-in-One Desk Reference For Dummies Rating: 5 out of 5 stars5/5
Reviews for Core Java
0 ratings0 reviews
Book preview
Core Java - Som Prakash Rai
Chapter 1
Introduction to Core Java
1.1 Introduction
Java is a high-level programming language which was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems’ Java platform (Java 1.0 [J2SE]). It runs on variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. The latest release of the Java Standard Edition is Java SE 9. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms.
1.2 Difference between C Programming and Java Programming
Fig 1.1: C Programming and Java Programming
C program will be saved with .c extension. After successful compilation of ‘c’ program native code or .exe file will be generated which is platform dependent and is cannot be used on any other platform. This .exe file is platform dependent because compatibility issue related to corresponding platform compatibility will be added. When we are writing java program, it must be saved with java extension. After successful compilation of java program .class file will be generated which is known as byte code, and it will be platform independent despite of corresponding platform JDK/JRE available. (Fig 1.1)
Q 1: Why in ‘C’ programming language native code is available where as in java native code is not available?
Ans: ‘C’ programming supports static loading which means if the corresponding member of program is required in further program then everything will not be loaded into the main memory where as Java supports dynamic loading which means if any method is required then it will be allocated in main memory and it will get immediately destroy after the use.
1.3 Important terms to remember
Source Code: The program which is written by the developer (Human Readable Format).
Byte Code: After successful compilation of java program byte code will be generated i.e. .class file, which is called as byte code. That byte code will be processed further while running the program.
Java Compiler: It is a program with the name of javac.exe which is mainly responsible for checking the grammar of the program language. It will also generate ‘.class’ file after the successful compilation of the program file with the name of class.
Interpreter: It is a program with the extension of java.exe. Interpreter is mainly responsible for generating byte code to the native code line by line. Converting byte code to native code is time consuming process. So in order to overcome this problem from java2 JIT complier has been introduced.
JIT (Just In Time) Compiler: It is used to convert byte code to the native code at a time by adding corresponding platform compatibly.
JVM (Java Virtual Machine): It provides platform independent execution environment.
Note: JVM itself is platform dependent. Byte Code is platform independent because of JVM available corresponding for that machine.
JRE (Java Runtime Environment): It provides many JVM in order to run the program. It comes in different version associated with the corresponding JDK.
JDK (Java Development Kit): It comes into different versions according to Sun Oracle released.
JDK 1.2 (Playground)
JDK 1.4 (Merline)
JDK 1.5 (Tiger)
JDK 1.6 (Mustang)
JDK 1.7 (Dolphin)
JDK 1.8 (Spider)
Note: JDK consist of different types of tools JRE, JVM in order to develop the java application.
Q 2: State whether following is True or False.
‘C’ programs are platform independent. (False)
Java programs are platform independent. (True)
‘C’ compiler is platform dependent. (True)
Java Compiler is platform Independent. (False)
JDK is platform Independent. (False)
1.4 Install the JDK
Steps
Open JTC DVD.
Open folder Java8.
Double click on JDK 1.8. (Fig 1.2)
Fig 1.2: Java SE Development Kit 8 Setup
Click on next.
Provide the destination folder where exactly you wanted to install JDK 1.8 or else if you do not want to specify any specific location then click on next. (Fig 1.3, 1.4)
Fig 1.3: Custom Setup
Fig 1.4: Progress
Click on next. (Fig 1.5, 1.6)
Fig 1.5: Destination Folder
Fig 1.6: Java Setup Progress
Click Finish. (Fig 1.7)
Fig 1.7: Java SE Development Kit 8 - Complete
After successful installation of JDK, you need to set path for the ‘bin’ of the JDK. It is not sufficient just to install JDK in your machine but parallel you need to set the JDK of bin.
1.4.1 Setting Path
Path can be set in two different ways:
Temporary path setting.
To set the Temporary path
Go to your corresponding working directory in command prompt.
Copy JDK/Bin folder location.
Type on command prompt. (Fig 1.8)
Set path =%path%;C:\Java\Jdk1.7\bin
Fig 1.8: Command Prompt
When you set path in temporary manner, then each time when you try to compile and run java program or when you open command prompt then you need to set the path, which is time consuming process.
In order to avoid the problem with temporary path setting we can set the path in permanent manner.
2. Permanent path setting.
To set the permanent path:
Right click on My Computer Icon.
Click on properties.
Click on advance tab. (Fig 1.9)
Fig 1.9: Advance System Settings
Click on Environment variable. (Fig 1.10)
Fig 1.10: Environment Variables
Click on new under user variable. (Fig 1.11)
Fig 1.11: New Variables
Provide variable name as path and variable value as path%;C:\Java\Jdk1.7\ bin (bin folder location)
Click on Ok. (Fig 1.12)
Fig 1.12: Edit User Variable
After setting the permanent path you need not to set the path again and again.
1.4.2 How to Compile Java Program
Write your java program and save that program with .java extension.
After successful compilation of java program (.class) file will be generate which is called byte code
with the name of class.
The entire java program must be written inside the class.
To compile java program open your command prompt.
Go to the corresponding working directory.
Syntax: javac filename (by which you have saved java program). E.g.: javac Jtc1.java
At the time of compiling the java program you need to provide the file name by which your java program has been saved.
You can save your java program by any name followed by ‘.java’ extension.
.class file will be generated with the name of class not with the name of the file name.
It is always recommendable to save the java program with the name of class which contains main method.
1.4.3 Running the java program
At the time of running the java program you need to provide the .class file name which contains main method. E.g.:- java Jtc1
Program 1.1
class Hello{
void m1(){
System.out.println(m1 in Hello
);
}
}
public class Jtc2 {
public static void main(String[] args) { Hello h1=new Hello(); h1.m1();
}}
Output:
Compile: javac Jtc2.java - Two .class file will be generated first with Hello and second with Jtc2.
Run : java Jtc2
M1 in Hello - output
Run : java Hello
Error : Main method not found in class Hello please define the main method as: Public static void main(String args[])
Above program can be saved by any name followed by .java extension while compiling the program you need to provide the file name by which you have saved the program.
After successful compilation of above program two .class file will be generated i.e. Jtc2 and Hello.
Note
The .class file generation does not depend upon by which you have saved program or main method. As many number of classes available inside your source file that many number of .class file will be generated.
While running the program you need to provide the (.class) file name which contains main method that is Jtc2.
Q 3: Let's consider that you are saving a java program by some name followed by .java extension but within that editor you have not written any single thing then what will happen when we compile it, will be considered as Java program or not?
Ans: It will be considered as java program but in order to compile successfully some context must be there inside, but if corresponding editor is not having any content then there is no question of compiling the program. So, even we cannot expect any .class file will be generated.
Q 4: Can we perform any kind of changes or any kind of replacement in byte code by the keyboard.
Ans: No, you cannot. If you are doing any changes in existing bytecode then you will not be able to run that program successfully because whatever input you are providing from the keyboard, it will be there in the form of input stream which is having some different meaning then the existing one.
Chapter 2
Java Language
2.1 Character Set
There are mainly following types of character set in java.
A to Z
a to z
0 to 9
Basically, these are all the special symbol available on your keyboard. If you are developing any application in ‘C’ programming language then it can support maximum English language because ‘C’ supports 8-bit ASCII character set. Whereas Java supports 16-bit UNICODE Character Set. So, by using Java as an application, it can support many languages.
When you are developing any application in Java then with the help of Internationalization (I18N) or localization. Till UNICODE 4.0 it was supporting almost all the languages but it was not supporting Sanskrit and some other languages. But from UNICODE 5.0 it supports Sanskrit and many more new inclusions are there.
2.2 Data Types
There are mainly two types of data type.
a) Primitive Data Type:
Program 2.1
class Hello{
byte b1;
short s1;
int i1;
long l1;
float f1;
double d1;
boolean b11;
char c1;
void m1(){
System.out.println(m1 in Hello
);
System.out.println(b1);
System.out.println(s1);
System.out.println(i1);
System.out.println(l1);
System.out.println(f1);
System.out.println(d1);
System.out.println(c1);
System.out.println(b11);
}
void m2(){
System.out.println(m2 in Hello
);
byte b11=127;
byte b22=-128;
//byte b33=128;
//byte b44=-129;
int i11=2147483647;
int i12=-2147483648;
//int i13=2147483648;
//long l14=2147483649;
long l12=2147483648l;
//float f11=11.11;
float f12=11.11f;
double d11=11.12;
double d12=11.13d;
System.out.println(b11);
System.out.println(b22);
//System.out.println(b33);
//System.out.println(b44);
System.out.println(i11);
System.out.println(i12);
//System.out.println(i13);
System.out.println(l12);
//System.out.println(f11);
System.out.println(f12);
System.out.println(d12);
System.out.println(d12);
}}
public class Jtc5 {
public static void main(String[] args) {
Hello h1=new Hello();
h1.m1();
h1.m2();
}
}
Output:
b) User Define Data Type:
There are mainly two types of user define data types:
Class
Interface
From JDK 1.5 two more data types have been introduced:
Enum
Annotation
2.3 Keywords
Keywords are having some predefine meaning which cannot be changed. In your further programme as keywords cannot be used as identifiers or user defined words.
List of Keywords:
2.4 Identifiers or User Define Words
Identifiers will be used as class variables methods etc. While naming the identifiers or user define words remember the following paths:
Keywords cannot be used as identifiers.
Identifier must not start with any numeric or it is always recommendable to starts some of the identifiers with small case.
It must not contain any special symbol other than (_ or $).
Identifier must not contain any blank space in between.
Identifiers may contain numeric but it should not be in starting.
e.g.:
int ab; //ok
int a_b; //ok
int a b; // not ok
Program 2.2
class Hello{
int a;
int ab;
int ab1;
//int 1ab; - not ok
int a1b;
int a_b;
int ab_;
int _ab;
int a$b;
//int _; - identifier might not be supported in releases after Java SE 8
int _$;
//int ab*; - not ok
//int ()_; - not ok
//int for; - not ok
//int true; - not ok
int Integer;
int INT;
int For;
void m1(){
System.out.println(m1 in Hello
);
System.out.println(Integer);
System.out.println(INT);
}
}
public class Jtc6 {
public static void main(String[] args) {
Hello h1=new Hello();
h1.m1();
}
}
Output:
Note: int Integer; Integer INT; int java.lang.Integer;
When we are writing int Integer, then it is being considered as variable of type int. This Integer
will not be considered as keyword.
When we are writing java.lang.Integer then in this case it is invalid declaration because special symbols are not allowed in identifiers name.
In java corresponding to the primitive data type wrapper classes has been designed which is available in java.lang package. Therefore in Integer int
, here Integer will be considered as wrapper class and ‘int’ will be considered as a reference variable of type Integer.
2.5 Variable
Variables will be of some specific data type which contains some value according to the compatibility of corresponding data type. The variable value can be changed in further program. The value which is been assign to any variable will be loosely coupled with that specific memory location.
The variable will be mainly of two types
Class Level Variable: It can be accessed anywhere within the class. If you are not assigning any value to the class level variable corresponding to data type, it will be assigned internally by the JVM.
Two class level variables cannot have same name.
The variable which is directly define inside the class is called as class level variable.
Local Variable: The variable which is define inside any block, method, constructor or inside any local context is called as local variable. Local variables will not be initialized implicitly by the JVM i.e. that local variable must be initialised explicitly by the developer before the use.
Local variable memory will be allocated only when that corresponding context is being processed and immediately after processing the context that memory will be destroyed. That is the region you cannot assess the local variable outside context.
You can name local variable and class level variable with the same name but when you are accessing that local variable in that context itself then that local variable only will be accessed. In order to access the class level variable within that local context you need use the this
keyword.
Program 2.3
class Hello{
int a=10;
int b;
int Hello;
Hello h1;
void m1(){
int ab=100;
int bc;
int a=101;
//int b;
System.out.println(m1 in Hello
);
System.out.println(a); System.out.println(b);
System.out.println(Hello);
System.out.println(h1);
System.out.println(ab);
//System.out.println(bc);
}
}
public class Jtc7 {
public static void main(String[] args) {
Hello h1=new Hello();
h1.m1();
}
}
Output:
2.6 Constant
If you wanted to declare any variable with the fixed value which must not change in further program. So declare that variable as final
.
final in tab=20;
static final int ab1=20;
static final int AB=30;
Program 2.4
class Hello{
int a=10;
final int ab=20;
static final int AB=30;
}
Hello h1= new Hello();
Hello h2= new Hello();
When you are writing final int ab then for ab, memory allocation depends upon number of objects creation. (Figure 2.1) In case of static final, int AB memory will be allocated only once for n number of object also.
According to JLS (Java Language Specification) static final variable must be in caps.
Final variable will not be initialized automatically by JVM explicitly, you must have to initialize final variable.
Q 1: Can we change the value of the final variable?
Ans: There are some conditions before changing the value of the final variable, such as if it is instance final variable and it being initialize there itself when you have declared then you will not be able to change the value of the final variable and If it is instance final variable and its not been initialized at the time of declaration then in different objects context you will be able to have the different values for the same final variable.
Q 2: Is it always necessary to initialize the value of the final variable at the time of declaration?
Ans: No, it is not necessary to initialize the value of final variable at the time of declaration. It can be initialized in following different ways:
At the time of declaration, With the help of block (if it is instance variable then we need to initialize it by using instance block whereas in the case of static variable we need to initialize explicitly in static block.
Also we can initialize it by using constructor.
2.7 Literals
Literals are the value which can be assigned to the corresponding type of data type variables. There are mainly following types of literals:
a) Integer Literal: Integer literal is further divided into following parts:
Decimal Literal: Decimal literal must not start with 0 i.e. it can be anything within 0 to 9.
e.g.
int i1=1234;
int i2=89746;
int i3=0123456; // not ok
In case of i3 it will give error i.e. if it is starting with zero then it will be considered as octal but octal must be within the range of 0 to 7.
Octal Literal: Octal literals must be within 0 to 7 but it must start with 0.
e.g.
int a=01234;
int b=01267;
int c=01248; // not ok
Hexadecimal literals: Hexadecimal literal must start with zerox (0X) or 0x and it must be within the range of 0 to 9 and A to F.
Note: a to f or A to F both are having the same meaning.
e.g.
int i1=0x1234A;
int i2=0X1234a;
int i3=1234a0X //not ok
It is not necessary that Hexadecimal literal must have 0X somewhere in between but it is parallelly important that the Hexadecimal no must start with OX.
Program 2.5
class Hello1{
int i1=123456980;
int i2=10100101;
int i3=2147483647;
int i4=-2147483648;
//int i5=2147483648;
int i6=0101001;
int i7=012347;
//int i8=0123478;
int i9=000;
int i10=0x123a;
int i11=0X123A;
int i12=0XABCF;
//int i13=123A0X;
long l1=2147483648l;
void m1(){
System.out.println(m1 in Hello
);
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
//System.out.println(i5);
System.out.println(i6);
System.out.println(i7);
//System.out.println(i8);
System.out.println(i9);
System.out.println(i10);
System.out.println(i11);
System.out.println(i12);
//System.out.println(i13);
System.out.println(l1);
}
}
public class Jtc9 {
public static void main(String[] args) {
Hello1 h1=new Hello1(); h1.m1();
}
}
Output:
b) Floating point Literal: In order to increase the readability from jdk1.7 underscore(_) is introduced. This (_) can be use to increase the readability of a numerical literal. Remember following points while using (_) in numeric literal:
Underscore(_) must not be used for any numeric literal must not start with underscore(_) and not end with (_).
Underscore must not be used just before or just after the decimal.
NOTE: This underscore is just for the developer site which will not reflect in output.
Program 2.6
class Hello2{
int i1=11234;
int i2=11_234;
int i3=11__234;
int i4=11_23_353_3;
//int i5=_1123;
//int i6=1123_;
int i7=0x123_a;
//int i8=0x_123_A;
int i9=01_01_01_01_0;
//int i10=0_X12233A; int i11=0_101010;
void m1(){
System.out.println(m1 in Hello
);
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
//System.out.println(i5);
//System.out.println(i6);
System.out.println(i7);
//System.out.println(i8);
System.out.println(i9);
//System.out.println(i10);
System.out.println(i11);
}
}
public class Jtc10 {
public static void main(String[] args) {
Hello2 h1=new Hello2();
h1.m1();
}
}
Output:
Floating point literal can be represented in two different ways:
Floating point representation.
e.g.- float f1=10.11; // not ok
Exponential representation.
double d1=9.9E+5;
double d2=9.9E-5;
Program 2.7
class Hai{
//float f1=11.22;
not ok float f2=11.22f;
float f3=11.33F;
float f4=Float.MAX_VALUE;
float f5=Float. MIN_VALUE;
//float f6=11.22;
float f7=11.22f;
float f8=99.999f;
float f9;
float f10;
double d1=11.555;
double d2=526.566D;
double d3=11.333d;
double d4=Double.MAX_VALUE;
double d5=Double.MIN_VALUE;
double d6=22.22;
double d7=221.378D;
double d8=99.99e+5;
double d9=9.9E-5;
void show(){
System.out.println(m1 in Hai
);
System.out.println(f2);
System.out.println(f3);
System.out.println(f4);
System.out.println(f5);
System.out.println(f7);
System.out.println(f8);
System.out.println(f9);
System.out.println(f10);
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
System.out.println(d4);
System.out.println(d5);
System.out.println(d6);
System.out.println(d7);
System.out.println(d8);
System.out.println(d9);
}
}
public class Jtc11 {
public static void main(String[] args) {
Hai h1=new Hai();
h1.show();
}}
Output:
F8 print Null as default (wrapper class) f9 print 0.0 because of primitive float type variable. Here f8 is called as reference variable and default type of a reference variable is null.
In case of f7 assignment allow from jdk 1.5 because of jdk 1.5 auto boxing and un-boxing. It automatically or internally performs the required casting. But if the same assignment you are doing in JDK1.4 then it will give the error.
c) Character literal: Anything which is enclosed with in single quotes will be considered as character literal.
e.g.-
char ch1=‘A’;
char ch2=‘a’;
char ch3=‘AB’ //not ok
char ch4=‘ ’; //not ok
In case of character literal whenever you are enclosing any escape sequence character then it will show its original value for what it has been created.
Program 2.8
class Hello{
char ch1; char ch2=‘ ’;
char ch3=‘ ’;
//char ch4=’’; not ok
//char ch5=‘ . ’; not ok
char ch6=‘"’;
//char ch7=‘’’; not ok
char ch8=‘\u0022’;
char ch9=‘A’;
//char ch10=‘AB’; not ok
char ch11=‘*’;
//char ch12=‘ ‘; not ok
//char ch13=‘123’; not ok
char ch14=‘1’;
char ch15=65;
char ch16=‘\n’;
char ch17=‘\t’;
char ch18=‘\r’;
char ch19=‘\\’;
//char ch20=‘\’;
//char ch21=‘//’;
char ch22=‘/’;
char ch23=‘.’;
char ch24=‘%’;
char ch25=‘\u0001’;
char ch26=‘\’’;
//char ch27=‘\u000ABC’;
int i1=‘A’;
//int i2=A;
//int i3=‘AB’;
int i4=‘9’;
int i5=‘\u0022’;
int i6=‘\\’;
//int i7=‘\’;
int i8=‘ ‘;
//int i9=‘123’;
void m1(){
System.out.println(m1 in Hello
);
System.out.println(ch1);
System.out.println(ch2);
System.out.println(ch3);
//System.out.println(ch4);
//System.out.println(ch5);
System.out.println(ch6);
//System.out.println(ch7);
System.out.println(ch8);
System.out.println(ch9);
//System.out.println(ch10);
System.out.println(ch11);
//System.out.println(ch12);
//System.out.println(ch13);
System.out.println(ch14);
System.out.println(ch15);
System.out.println(ch16);
System.out.println(ch17);
System.out.println(ch18);
System.out.println(ch19);
//System.out.println(ch20);
//System.out.println(ch21);
System.out.println(ch22);
System.out.println(ch23);
System.out.println(ch24);
System.out.println(ch25);
System.out.println(ch26);
//System.out.println(ch27);
System.out.println(i1);
//System.out.println(i2);
System.out.println(i4);
System.out.println(i5);
System.out.println(i6);
System.out.println(i8);
}
}
public class Jtc12 {
public static void main(String[] args) {
Hello h1=new Hello();
h1.m1();
}
}
Output:
Program 2.9
public class Jtc13 {
public static void main(String[] args) {
char ch1;
char ch2=‘ ‘;
//char ch3=‘’;
//char ch4=‘NULL’;
//char ch5=Null;
//char ch6=null;
Character ch7=null;
//System.out.println(ch1);
System.out.println(ch2);
//System.out.println(ch3);
//System.out.println(ch4);
//System.out.println(ch5);
//System.out.println(ch6);
System.out.println(ch7);
}
}
Output:
Exception in thread main
java.lang.Error: Unresolved compilation problem: The local variable ch1 may not have been initialized System.out.println(ch1);
NOTE
In java there is no empty character literal but when you are assigning any character literal without giving any space in single quotes then it will be considered as empty character literal and it will give error at compile time.
Commenting line dosen't mean that the compiler is going to ignore that. It reads that line by converting in corresponding Unicode but again it is not going to be verified from compiler library.
In jtc13 we have written character ch6=null it doesn't mean that it is a character literal but in facts ch6 is a reference variable which is available inside main method & in that explicitly we have assigned default value of