'Java Crash Cource Book'
'Java Crash Cource Book'
The code given to the assembler cannot be executed directly but after it is
translated it can be easily executes.
High -level language: this is the new generation of programming
languages in which we use English words to write code making it easier for
us to write our code. It is easy to learn and use. It is machine independent
Page 1 of 17
so that your program will run on different. Each language instruction is
called a ‘statement’. A program written in high level language is called a
‘source code' or ‘source program'. In this language to add 2 and 3, we must
write (in Java only):
Result = to 2 + 3;
Here we are assigning result to store a sum of two and three which is 5 but
the way of writing a code is very easy to calculate 2 + 3 and allocates it to
to the memory of ‘result’.
Page 2 of 17
Anatomy of a Java program
Class structure
Class class_name{
Code block
}
We will use a class keyword and then we will put under scroll and then
specify the name of our class (names can be anything). After this put a pair
of curly braces inside which we will type a code.
Methods
These are a group of instructions to do a specific task. We cannot create
method without a class.
Syntax –
return_type method_name(parameters){
Code block
}
After calling a method, the method is executed. In this case 'Hi Tom' will be
printed.
Page 3 of 17
main() is automatically called when we run a Java program. It is the starting
point of execution of a Java program. All the other methods are called in
the main () method only. We cannot have a Java program without a
main method.
So why now you might have got the idea that whenever we need to create
a method it must be contained in a class.
Access Modifiers
These are the keywords with specified how to use classes and methods.
We have the following keywords (Access Modifiers) in Java –
Public
Private
Protected
Default
The above keywords are used in the basic Java program. These will be
discussed in detail later in this book. For now just keep in mind that in Java
we have public, private, protected and default keywords. These keywords
are access modifiers in Java.
Naming Conventions
Using the pascal case convention every letter of the first word will be
capital.this convention is used for classes in Java. So, we must use Pascal
case convention for writing the names of our classes in Java.
Page 4 of 17
the starting word must be in lower case.This type of convention is used for
naming variables and methods in Java. We will talk about variables later
but what I want to make you know is that we use this type of convention in
naming variables.
Example –
thisIsAName
So first of all we write the access modifier which is public and this case and
then write the class keyword after which we write the name of our class and
curly braces. All our code would be written in the parentheses. As we know
we have to create a main method in every class so we have to write the
code for main method which is as follows
Here we have a public key word which is access modifier than static
keyword which is non access modifier(we will talk about non access
modifiers later in OOPs).void is the return type, then we put the name, then
we open parentheses inside which we have parameters.(String[]args) is the
data that we can give the main method to work with. After this we open the
Page 5 of 17
curly braces in which we write our code to be executed when are main
method runs, this is called the body of the main method. This is a simple
java program and when we create our first program you will find that
program exactly like this.
The name of our class is named according to pascal case convention
and the name of our method is named according to camel case
convention.
We cannot change the name of the main method, all its letters must
be in lowercase.
Packages
Page 6 of 17
Introduction and History of Java
Java is an object-oriented programming language developed by James
Gosling and colleagues at Sun Microsystems in the early 1990s. Unlike
conventional languages which are generally designed either to be compiled
to native (machine) code, or to be interpreted from source code at runtime,
Java is intended to be compiled to a bytecode, which is then run by a Java
Virtual Machine.Most of the syntaxes of Java are similar to that of C and C+
+.
Java was started as a project called "Oak" by James Gosling in June 1991.
Gosling's goals were to and simplicity than C/C++. The first public
implementation was Java 1.0 in 1995. It made the promise of "Write Once,
Run Anywhere", with free runtimes on popular platforms. It was fairly
secure and its security was configurable, allowing for network and file
access to be limited. The major web browsers soon incorporated it into
their standard configurations in a secure "applet" configuration. popular
quickly. New versions for large and small platforms (J2EE and J2ME) soon
were designed with the advent of "Java 2". Sun has not announced any
plans for a "Java 3".
In 1997, Sun approached the ISO/IEC JTC1 standards body and later the
Ecma International to formalize Java, but it soon withdrew from the
process. Java remains a proprietary de facto standard that is controlled
through the Java Community Process. Sun makes most of its Java
implementations available without charge, with revenue being generated by
specialized products such as the Java Enterprise System. Sun
distinguishes between its Software Development Kit (SDK) and Runtime
Environment (JRE) which is a subset of the SDK, the primary distinction
being that in the JRE the compiler is not present.
There were five primary goals in the creation of the Java language:
Page 7 of 17
2. It should allow the same program to be executed on multiple operating
systems.
3. It should contain built-in support for using computer networks.
4. It should be designed to execute code from remote sources securely.
5. It should be easy to use by selecting what was considered the good
parts of other object-oriented languages.
Page 8 of 17
Comparing Java and C++, it is possible in C++ to implement similar
functionality (for example, a memory management model for specific
classes can be designed in C++ to improve speed and lower memory
fragmentation considerably), with the possible cost of extra development
time and some application complexity. In Java, garbage collection is built-in
and virtually invisible to the developer. That is, developers may have no
notion of when garbage collection will take place as it may not necessarily
correlate with any actions being explicitly performed by the code they write.
Depending on intended application, this can be beneficial or
disadvantageous: the programmer is freed from performing low-level tasks,
but at the same time loses the option of writing lower level code.
The syntax of Java is largely derived from C++. However, unlike C++,
which combines the syntax for structured, generic, and object-oriented
programming, Java was built from the ground up to be virtually fully object-
oriented: everything in Java is an object with the exceptions of atomic
datatypes (ordinal and real numbers, boolean values, and characters) and
everything in Java is written inside a class.
The Java Runtime Environment or JRE is the software required to run any
application deployed on the Java Platform. End-users commonly use a JRE
in software packages and Web browser plugins. Sun also distributes a
superset of the JRE called the Java 2 SDK (more commonly known as the
JDK), which includes development tools such as the Java compiler,
Javadoc, and debugger.
Features of Java
Object Oriented
Platform Independent
Simple and Secure
Architectural Neutral
Portable
Robust
Multithreaded
Interpreted
High Performance
Distributed
Dynamic
Page 9 of 17
Java Environment Setup
There are certain requirements of hardware to run a java program.
After installing the JDK you need to set the path in your computer. Here are
the steps:
Right click on ‘My Computer’ and select “Properties”
Click on the ‘Environment variables’ button under the
‘Advanced’ tab.
Now , alter the path variable so that it also contains the path to
the Java executable. Example, if your path is currently set to
Now after installing the JDK and setting the path , you must install Eclipse
IDE or you can also use any other IDE like IntelliJ or even Notepad.
Page 10 of 17
Open any browser.
Search ‘free download eclipse ide’
Open the one which is provided to you by eclipse.com
Download the version according to your system like x64/x32 bit
An exe file will be installed
Run the file
Here you have to select Éclipse IDE for java developers’
Click on install.
Now you are good to go.
Page 11 of 17
Identifiers are the names of variables , methods , classes, packages
and interfaces(all of these will be covered in further chapters).
A whitespace is a term used in Java to describe blanks , tabs ,
newline characters and comments. These are totally ignored by the
java compiler. Example – int age;
The space between “int” and “age” is called a whitespace.
Comments are like helping text in a Java program. In real world
scenario , there are many people work for the same project , hence
writing comments helps other people understand what you have
written.
There are two types of comments :
1.Single comments are denoted by 2 ” // ” slashes.
2.Multi comments are denoted by “/*“ and “*/” where “/*“ is used
to denote start and the other is used to denote end.
Page 12 of 17
By the above statement we can only use the methods of the class Student
but not all the classes present in the test package. To do that we must write
the code as - import test.*;
Page 13 of 17
You can think of variables as container. this is because in programming
when you talk about software it is all about data. Hence, when you build
software you are processing a lot of data. Now the question arises where to
store the data? the answer is we have a special kind of thing which is
called variable for storing our data. This basic concept is given in maths it is
the same here also you can even change the value of the data stored in a
variable.
For example if you want to store a pencil, you can keep it in a pouch, in
your bag, in your pocket or somewhere else. So these things like your
pocket, pouch or your bag are variables.
From the table you will come to know that different values of data must be
stored and different types of data types. For example if you want to store a
value of let say 124 , the data type that you must use is character here, you
can use integers also. I don't prefer using integer for such a small number
because it it requires more space than characters.
For assigning a value to the variable first we must write the data type that
we want to use then write the name of the variable that we need but it could
be anything after which we have to put a equals to sign and specify the
value that we want our variable to get assigned. For example -
char a = 8;
Page 14 of 17
Here char is a data type, a is the name of variable and 8 is the value
assigned.
We can assign a integer value to a float value and also to double value but
we cannot assign a double value to integer value. It is just like putting a
pouch in a pen. For example if you want to store a double value let's say
2.4 in the integer data type then we have to lose some data , here we will
lose .4 . So, the final integer value would be to 2. These all data types are
called as primitive datatypes.
variables can be re-used that is one I declare a variable I can use it again
and again in my program and I can even modify it afterwards in the
program. For example if I create a variable a which is containing a value 5
and 8 then I can reuse it by printing the variable a ,i.e.,
System.out.println(a); // output - 5
Now if you want to assign a value, let say 5, to a variable using the integer
data type then it will then it will consume 4 bytes of your memory instead
you can use a term called as short which basically means that it is a short
integer as it requires only to bytes of your memory. The range of a short is -
32768 to 32767. Using this short you can save to bytes of your memory.
Now using short for storing the value 5 would also would also be more
space consuming, to solve this problem you can use byte which takes only
one byte of your memory. The range for byte is –128 to 127.
If you have a number which is let say is of 8 to 10 digits then you have to
use long. when you initialize a number to long data type then you have to
write a letter L after the number. For example, you can write long x =
500000000L;
For storing a single character in a variable such as P we use the data type
called as char. It is used to assign the value of a single character only.
For example – char x = 'D’;
Page 15 of 17
If we store the value 5 using a double data type then it would be
automatically converted into a double For example , double b5 = 5; . This
is called as implicit conversion where the value automatically gets
converted into other data type. If we see the output of the above example
then we will get the output 5.0 this is because double supports float values
as well. So if we assign it to integer value it would show point zero as well.
Byte values can be casted into short , short and character values can be
assigned to an integer, integer can be assigned to long, long can be
assigned to float data type and float data type can be assigned to a double
data type.
Page 16 of 17
Operators in Java
Page 17 of 17