JAVA
JAVA
JAVA essentials :
Tools You Will Need
For performing the examples discussed in this tutorial, you will need a
Pentium 200-MHz computer with a minimum of 64 MB of RAM (128
MB of RAM recommended).
You will also need the following softwares:
Linux 7.1 or Windows xp/7/8 operating system
Java JDK 8
Microsoft Notepad or any other text editor
a – Basic Syntax
Now, alter the 'Path' variable so that it also contains the path to the
Java executable. Example, if the path is currently set to
'C:\WINDOWS\SYSTEM32', then change your path to read
'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.
Basic Syntax :
Class Names - For all class names the first letter should be in Upper
Case. If several words are used to form a name of the class, each inner
word's first letter should be in Upper Case.
Java Variables
Java Arrays
Arrays are objects that store multiple variables of the same type.
However, an array itself is an object on the heap. We will look into how
to declare, construct, and initialize in the upcoming chapters.
Java Modifiers
Like other languages, it is possible to modify classes, methods, etc., by
using modifiers. There are two categories of modifiers:
Access Modifiers: default, public , protected, private
Java Enums
Enums were introduced in Java 5.0. Enums restrict a variable to have
one of only a few predefined values. The values in this enumerated list
are called enums.
With the use of enums it is possible to reduce the number of bugs in
your code.
For example, if we consider an application for a fresh juice shop, it
would be possible to restrict the glass size to small, medium, and large.
This would make sure that it would not allow anyone to order any size
other than small, medium, or large.
Example
class FreshJuice {
enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
FreshJuiceSize size;
}
public class FreshJuiceTest {
public static void main(String args[]){
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice.FreshJuiceSize.MEDIUM ;
System.out.println("Size: " + juice.size);
}
}
The above example will produce the following result:
Size: MEDIUM
Note: Enums can be declared as their own or inside a class. Methods,
variables, constructors can be defined inside enums as well.
Java Keywords
JVMs are available for many hardware and software platforms (i.e. JVM
is platform dependent).
What is JVM
It is:
1. A specification where working of Java Virtual Machine is
specified. But implementation provider is independent to choose
the algorithm. Its implementation has been provided by Oracle
and other companies.
2. An implementation Its implementation is known as JRE (Java
Runtime Environment).
3. Runtime Instance Whenever you write java command on the
command prompt to run the java class, an instance of JVM is
created.
What it does
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
o Memory area
o Class file format
o Register set
o Garbage-collected heap
o Fatal error reporting etc.
JVM Architecture
Output:
sun.misc.Launcher$AppClassLoader@4e0e2f2a
null
2) Class(Method) Area
3) Heap
4) Stack
Java Stack stores frames. It holds local variables and partial results, and
plays a part in method invocation and return.
Each thread has a private JVM stack, created at the same time as
thread.
7) Execution Engine
It contains:
1. A virtual processor
2. Interpreter: Read bytecode stream then execute the instructions.
3. Just-In-Time(JIT) compiler: It is used to improve the performance.
JIT compiles parts of the byte code that have similar functionality
at the same time, and hence reduces the amount of time needed
for compilation. Here, the term "compiler" refers to a translator
from the instruction set of a Java virtual machine (JVM) to the
instruction set of a specific CPU.
Features of JAVA :
Object Oriented: In Java, everything is an Object. Java can be easily
extended since it is based on the Object model.
Platform Independent: Unlike many other programming languages
including C and C++, when Java is compiled, it is not compiled into
platform specific machine, rather into platform independent byte code.
This byte code is distributed over the web and interpreted by the
Virtual Machine (JVM) on whichever platform it is being run on.
Simple: Java is designed to be easy to learn. If you understand the
basic concept of OOP Java, it would be easy to master.
Secure: With Java's secure feature it enables to develop virus-free,
tamper-free systems. Authentication techniques are based on public-
key encryption.
Architecture-neutral: Java compiler generates an architecture-neutral
object file format, which makes the compiled code executable on many
processors, with the presence of Java runtime system.
Portable: Being architecture-neutral and having no implementation
dependent aspects of the specification makes Java portable. Compiler
in Java is written in ANSI C with a clean portability boundary, which is a
POSIX subset.
Robust: Java makes an effort to eliminate error prone situations by
emphasizing mainly on compile time error checking and runtime
checking.
Multithreaded: With Java's multithreaded feature it is possible to
write programs that can perform many tasks simultaneously. This
design feature allows the developers to construct interactive
applications that can run smoothly.
Interpreted: Java byte code is translated on the fly to native machine
instructions and is not stored anywhere. The development process is
more rapid and analytical since the linking is an incremental and light-
weight process.
High Performance: With the use of Just-In-Time compilers, Java
enables high performance.
// Stages
// Main class
class GFG {
// Print command
System.out.print("Welcome to
Geeks");
Output
Welcome to Geeks
Let us understand the real compilation and execution process.
Step 1: Let us create a file writing simple printing code in a text file and
saving it with “.java” extension.
Step 2: Open the terminal(here we are using macOS)and go to the
Desktop directory using the below command as follows.
cd /Users/mayanksolanki/GFG.java
Step 3: Let us try to compile our program with the below command
javac GFG.java
Step 4: Lastly run it with the below command as follows:
java GFG
Note: GFG.class file is created after the third step which means that
now our entire code in the java programming language is secure
encrypted as it contains only binary. In step 4 we are running that file.
Refer to the below media for ease of understanding.
Data Types in Java
Data types specify the different sizes and values that can be stored in
the variable. There are two types of data types in Java:
In Java language, primitive data types are the building blocks of data
manipulation. These are the most basic data types available in Java
language.
The Boolean data type specifies one bit of information, but its "size"
can't be defined precisely.
Example:
The byte data type is an example of primitive data type. It isan 8-bit
signed two's complement integer. Its value-range lies between -128 to
127 (inclusive). Its minimum value is -128 and maximum value is 127.
Its default value is 0.
The byte data type is used to save memory in large arrays where the
memory savings is most required. It saves space because a byte is 4
times smaller than an integer. It can also be used in place of "int" data
type.
Example:
The short data type is a 16-bit signed two's complement integer. Its
value-range lies between -32,768 to 32,767 (inclusive). Its minimum
value is -32,768 and maximum value is 32,767. Its default value is 0.
The short data type can also be used to save memory just like byte data
type. A short data type is 2 times smaller than an integer.
Example:
The int data type is a 32-bit signed two's complement integer. Its value-
range lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1)
(inclusive). Its minimum value is - 2,147,483,648and maximum value is
2,147,483,647. Its default value is 0.
The int data type is generally used as a default data type for integral
values unless if there is no problem about memory.
Example:
The long data type is a 64-bit two's complement integer. Its value-range
lies between -9,223,372,036,854,775,808(-2^63) to
9,223,372,036,854,775,807(2^63 -1)(inclusive). Its minimum value is -
9,223,372,036,854,775,808and maximum value is
9,223,372,036,854,775,807. Its default value is 0. The long data type is
used when you need a range of values more than those provided by int.
Example:
The float data type is a single-precision 32-bit IEEE 754 floating point.Its
value range is unlimited. It is recommended to use a float (instead of
double) if you need to save memory in large arrays of floating point
numbers. The float data type should never be used for precise values,
such as currency. Its default value is 0.0F.
Example:
1. float f1 = 234.5f
Example:
1. double d1 = 12.3
The char data type is a single 16-bit Unicode character. Its value-range
lies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char
data type is used to store characters.
Example:
o Documentation Section
o Package Declaration
o Import Statements
o Interface Section
o Class Definition
o Class Variables and Variables
o Main Method Class
o Methods and Behaviors
Documentation Section
1. /*It is an example of
2. multiline comment*/
o Documentation Comment: It starts with the delimiter (/**) and
ends with */. For example:
Package Declaration
Import Statements
Interface Section
1. interface car
2. {
3. void start();
4. void stop();
5. }
Class Definition
In this section, we define the main() method. It is essential for all Java
programs. Because the execution of all Java programs starts from the
main() method. In other words, it is an entry point of the class. It must
be inside the class. Inside the main method, we create objects and call
the methods. We use the following statement to define the main()
method:
For example:
You can read more about the Java main() method here.
When we follow and use the above elements in a Java program, the
program looks like the following.
CheckPalindromeNumber.java
Output: