Introduction To Java
Introduction To Java
Applications of Java
Top 10 Applications of Java in Real World
Java is the Queen of programming languages. It beholds the pride
of the most
its 25th anniversary on 23rd May 2020 and is continuously
updating to cope with the latest technological advancements. It
is surprising to note that at present around three billion
devices use Java for development. Java has an amazing design
that incorporates the flexibility to run on any machine. It has
been a major component in the development of a plethora of
applications and the advancement of major technologies.
Java has become the most robust programming language because of
its amazing features. Some of its features are platform
independence, high performance, Object orientation, support for
automatic garbage management, and many more. In this blog, we
will discuss the Top 10 Applications of Java in the real world
in a detailed manner.
• Desktop GUI Applications
• Mobile Applications
• Artificial intelligence
• Web applications
• Big Data technology
• Gaming applications
• Business applications
• Embedded systems
• Cloud applications
• Scientific applications
So let’s get started.
1. Desktop GUI Applications
2. Mobile Applications
3. Artificial Intelligence
4. Web Applications
6. Gaming Applications
7. Business Applications
8. Embedded Systems
9. Cloud Applications
History of Java
•
Java is an Object-Oriented programming language developed
by James Gosling in the early 1990s. The team initiated this
project to develop a language for digital devices such as set-
top boxes, television, etc. Originally C++ was considered to be
used in the project but the idea was rejected for several
reasons(For instance C++ required more memory). Gosling
endeavoured to alter and expand C++ however before long
surrendered that for making another stage called Green. James
Gosling and his team called their project “Greentalk” and its
file extension was .gt and later became to known as “OAK”.
Why “Oak”?
The name Oak was used by Gosling after an oak tree that
remained outside his office. Also, Oak is an image of
solidarity and picked as a national tree of numerous nations
like the U.S.A., France, Germany, Romania, etc. But they had to
later rename it as “JAVA” as it was already a trademark by Oak
Technologies. “JAVA” Gosling and his team did a brainstorm
session and after the session, they came up with several names
such as JAVA, DNA, SILK, RUBY, etc. Java name was decided after
much discussion since it was so unique. The name Java
originates from a sort of espresso bean, Java. Gosling came up
with this name while having a coffee near his office. Java was
created on the principles like Robust, Portable, Platform
Independent, High Performance, Multithread, etc. and was called
one of the Ten Best Products of 1995 by the TIME MAGAZINE.
Currently, Java is used in internet programming, mobile
devices, games, e-business solutions, etc.
The Java language has experienced a few changes since JDK
1.0 just as various augmentations of classes and packages to
the standard library. In Addition to the language changes,
considerably more sensational changes have been made to the
Java Class Library throughout the years, which has developed
from a couple of hundred classes in JDK 1.0 to more than three
thousand in J2SE 5.History of various Java versions:
Version Release Date Major changes
• A broad retooling of
the AWT occasion show
• Inner classes added to
the language
• JavaBeans
• JDBC
• RMI
Additions: Library
improvements
• Regular expressions
modelled after Perl
regular expressions
• The image I/O API for
reading and writing
images in formats like
JPEG and PNG
• Integrated XML parser
and XSLT processor
(JAXP) (specified in
JSR 5 and JSR 63)
• Preferences API
(java.util.prefs)
Public Support and security
updates for this version
ended in October 2008.
• Upgrade of JAXB to
version 2.0: Including
integration of a StAX
parser.
• Support for pluggable
annotations (JSR 269).
• JDBC 4.0 support (JSR
221)
• Additional Unicode
language-tag
extensions
• Root certificates
• Thread-local
handshakes
• Heap allocation on
alternative memory
devices
• Remove the native-
header generation tool
– javah.
• Consolidate the JDK
forest into a single
repository.
Comments in Java
•
In a program, comments are like indents one makes, they are used
so that it is easier for someone who isn’t familiar with the
language to be able to understand the code. It will also make
the job easier for you, as a coder, to find errors in the code
since you will be easily able to find the location of the bug.
Comments are ignored by the compiler while compiling a code,
which makes the job more complex in the long run when they have
to go through so much code to find one line.
1. Single-line comments.
2. Multi-line comments.
3. Documentation comments.
A. Single-line comments
A beginner-level programmer uses mostly single-line comments for
describing the code functionality. It’s the easiest typed
comments.
Syntax:
//Comments here( Text in this line only is considered as
comment )
Example:
class Scomment
{
public static void main(String args[])
{
// Single line comment here
System.out.println("Single line comment above");
}
}
Output:
Single line comment above
B. Multi-line Comments:
To describe a full method in a code or a complex snippet single
line comments can be tedious to write since we have to give
‘//’ at every line. So to overcome this multi-line comments can
be used.
Syntax:
/*Comment starts
continues
continues
.
.
.
Comment ends*/
Example:
• Java
//Java program to show multi line comments
class Scomment
{
public static void main(String args[])
{
System.out.println("Multi line comments below");
/*Comment line 1
Comment line 2
Comment line 3*/
}
}
Output:
Multi line comments below
We can also accomplish single line comments by using the above
syntax as shown below:
/*Comment line 1*/
C. Documentation Comments:
This type of comment is used generally when writing code for a
project/software package, since it helps to generate a
documentation page for reference, which can be used for getting
information about methods present, its parameters, etc. For
example, http://docs.oracle.com/javase/7/docs/api/java/util/Sca
nner.html is an auto-generated documentation page that is
generated by using documentation comments and a javadoc tool
for processing the comments.
Syntax:
/**Comment start
*
*tags are used in order to specify a parameter
*or method or heading
*HTML tags can also be used
*such as <h1>
*
*comment ends*/
class GFG {
public static void main (String[] args) {
/**
comment line 1
comment line 2
*/
}
}
Literals in java
Literal: Any constant value which can be assigned to the
variable is called literal/constant.
In simple words, Literals in Java is a synthetic representation
of boolean, numeric, character, or string data. It is a medium
of expressing particular values in the program, such as an
integer variable named ‘’/count is assigned an integer value in
the following statement.
// Here 100 is a constant/literal.
int x = 100;
Integral literals
Output
101
64
64206
15
Note : By default, every literal is of int type, we can specify
explicitly as long type by suffixed with l or L. There is no
way to specify byte and short literals explicitly but
indirectly we can specify. Whenever we are assigning integral
literal to the byte variable and if the value is within the
range of byte then the compiler treats it automatically as byte
literals.
Floating-Point literal
• Java
Output
101.230
123.222
Error: malformed floating point literal
Note: By default, every floating-point literal is of double
type, and hence we cant assign directly to the float variable.
But we can specify floating-point literal as float type by
suffixed with f or F. We can specify explicitly floating-point
literal as double type by suffixed with d or D. Of course this
convention is not required.
Char literals
char ch = 'a';
char b = 0789;
// Unicode representation
char c = '\u0061';
System.out.println(ch);
System.out.println(b);
System.out.println(c);
System.out.println("\" is a symbol");
Output
a
error:Integer number too large
a
" is a symbol
String literals
• Java
Output
Hello
error: cannot find symbol
symbol: variable Hello
location: class Test
Boolean literals
Only two values are allowed for Boolean literals, i.e., true
and false.
boolean b = true;
• Java
Output
true
false
error: incompatible types: int cannot be converted to boolean
error: incompatible types: int cannot be converted to boolean
Note: When we are performing concatenation operations, then the
values in brackets are concatenated first. Then the values are
concatenated from the left to the right. We should be careful
when we are mixing character literals and integers in String
concatenation operations and this type of operation are known
as Mixed Mode operation.
• Java
// ASCII value of 0 is 48
// ASCII value of 7 is 55
System.out.println("Geeks!" + first +
'2' + second);
Output
Geeks!48255
Explanation: Whenever we are performing addition between a
string and integer, the overall result is converted into a
string. The above program evaluation is done in the following
way:
"Geeks!" + first + '2' + second
"Geeks! " + 48 + '2' + 55
"Geeks!48" + '2' + 55
"Geeks!482" + 55
"Geeks!48255"
Keywords in java
Java Identifiers
•
In Java, identifiers are used for identification purposes. Java
Identifiers can be a class name, method name, variable name, or
label.
Example of Java Identifiers
public class Test
{
public static void main(String[] args)
{
int a = 20;
}
}
In the above Java code, we have 5 identifiers namely :
• Test: class name.
• main: method name.
• String: predefined class name.
• args: variable name.
• a: variable name.
Rules For Defining Java Identifiers
There are certain rules for defining a valid Java identifier.
These rules must be followed, otherwise, we get a compile-time
error. These rules are also valid for other languages like C,
and C++.
• The only allowed characters for identifiers are all
alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘(dollar
sign) and ‘_‘ (underscore).For example “geek@” is not a
valid Java identifier as it contains a ‘@’ a special
character.
• Identifiers should not start with digits([0-9]). For
example “123geeks” is not a valid Java identifier.
• Java identifiers are case-sensitive.
• There is no limit on the length of the identifier but
it is advisable to use an optimum length of 4 – 15
letters only.
• Reserved Words can’t be used as an identifier. For
example “int while = 20;” is an invalid statement as a
while is a reserved word. There are 53 reserved words
in Java.
Examples of valid identifiers :
MyVariable
MYVARIABLE
myvariable
x
i
x1
i1
_myvariable
$myvariable
sum_of_array
geeks123
Note: The keywords const and goto are reserved, even though
they are not currently used. In place of const, the final
keyword is used. Some keywords like strictfp are included in
later versions of Java.
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
AD
1. //First Java Program
o Multi-line Comment: It starts with a /* and ends
with */. We write between these two symbols. For example:
1. /*It is an example of
2. multiline comment*/
o Documentation Comment: It starts with the
delimiter (/**) and ends with */. For example:
1. interface car
2. {
3. void start();
4. void stop();
5. }
Class Definition
For example:
You can read more about the Java main() method here.
Methods and behavior
When we follow and use the above elements in a Java program, the
program looks like the following.
CheckPalindromeNumber.java
Output:
Java Variables
Read
Practice
Video
•
In Java, Variables are the data containers that save the data
values during Java program execution. Every Variable in Java is
assigned a data type that designates the type and quantity of
value it can hold. A variable is a memory location name for the
data.
Variables in Java
Java variable is a name given to a memory location. It is the
basic unit of storage in a program.
• The value stored in a variable can be changed during
program execution.
• Variables in Java are only a name given to a memory
location. All the operations done on the variable
affect that memory location.
• In Java, all variables must be declared before use.
How to Declare Variables in Java?
We can declare variables in Java as pictorially depicted below
as a visual aid.
From the image, it can be easily perceived that while declaring
a variable, we need to take care of two things that are:
1. datatype: Type of data that can be stored in this
variable.
2. data_name: Name was given to the variable.
In this way, a name can only be given to a memory location. It
can be assigned values in two ways:
• Variable Initialization
• Assigning value by taking input
How to Initialize Variables in Java?
It can be perceived with the help of 3 components that are as
follows:
• datatype: Type of data that can be stored in this
variable.
• variable_name: Name given to the variable.
• value: It is the initial value stored in the variable.
Illustrations:
// Declaring float variable
float simpleInterest;
// Declaring and initializing integer variable
int time = 10, speed = 20;
// Declaring and initializing character variable
char var = 'h';
Types of Variables in Java
Now let us discuss different types of variables which are
listed as follows:
1. Local Variables
2. Instance Variables
3. Static Variables
Let us discuss the traits of every type of variable listed here
in detail.
1. Local Variables
A variable defined within a block or method or constructor is
called a local variable.
• These variables are created when the block is entered,
or the function is called and destroyed after exiting
from the block or when the call returns from the
function.
• The scope of these variables exists only within the
block in which the variables are declared, i.e., we can
access these variables only within that block.
• Initialization of the local variable is mandatory
before using it in the defined scope.
Time Complexity of the Method:
Time Complexity: O(1)
Auxiliary Space: O(1)
Below is the implementation of the above approach:
• Java
// Local Variables
import java.io.*;
class GFG {
Output
Local Variable: 10
Example :
• Java
package a;
// x is a local variable
int x = 10;
// variable
String message = "Hello, world!";
if (x > 5) {
// result is a
// local variable
System.out.println(result);
String loopMessage
= "Iteration "
// compile-time error
// System.out.println(loopMessage);
Output :
message = Hello, world!
x is greater than 5
Iteration 0
Iteration 1
Iteration 2
2. Instance Variables
Instance variables are non-static variables and are declared in
a class outside of any method, constructor, or block.
• As instance variables are declared in a class, these
variables are created when an object of the class is
created and destroyed when the object is destroyed.
• Unlike local variables, we may use access specifiers
for instance variables. If we do not specify any access
specifier, then the default access specifier will be
used.
• Initialization of an instance variable is not
mandatory. Its default value is dependent on the data
type of variable. For String it
is null, for float it is 0.0f, for int it is 0, for
Wrapper classes like Integer it is null, etc.
• Instance variables can be accessed only by creating
objects.
• We initialize instance variables
using constructors while creating an object. We can
also use instance blocks to initialize the instance
variables.
The complexity of the method:
Time Complexity: O(1)
Auxiliary Space: O(1)
Below is the implementation of the above approach:
• Java
// Instance Variables
import java.io.*;
class GFG {
public int i;
public Integer I;
public GFG()
// Default Constructor
// Main Method
// Object Creation
// Displaying O/P
+ name.i);
+ name.I);
}
Output
Geek name is: Shubham Jain
Default value for int is 0
Default value for Integer is null
3. Static Variables
Static variables are also known as class variables.
• These variables are declared similarly to instance
variables. The difference is that static variables are
declared using the static keyword within a class
outside of any method, constructor, or block.
• Unlike instance variables, we can only have one copy of
a static variable per class, irrespective of how many
objects we create.
• Static variables are created at the start of program
execution and destroyed automatically when execution
ends.
• Initialization of a static variable is not mandatory.
Its default value is dependent on the data type of
variable. For String it is null, for float it is 0.0f,
for int it is 0, for Wrapper classes like Integer it
is null, etc.
• If we access a static variable like an instance
variable (through an object), the compiler will show a
warning message, which won’t halt the program. The
compiler will replace the object name with the class
name automatically.
• If we access a static variable without the class name,
the compiler will automatically append the class name.
But for accessing the static variable of a different
class, we must mention the class name as 2 different
classes might have a static variable with the same
name.
• Static variables cannot be declared locally inside an
instance method.
• Static blocks can be used to initialize static
variables.
The complexity of the method:
Time Complexity: O(1)
Auxiliary Space: O(1)
Below is the implementation of the above approach:
• Java
// Java Program to demonstrate
// Static variables
import java.io.*;
class GFG {
// static variable
// declared locally.
Output
Geek Name is : Shubham Jain
Differences Between the Instance Variables and the Static
Variables
Now let us discuss the differences between the Instance
variables and the Static variables:
• Each object will have its own copy of an instance
variable, whereas we can only have one copy of a static
variable per class, irrespective of how many objects we
create. Thus, static variables are good
for memory management.
• Changes made in an instance variable using one object
will not be reflected in other objects as each object
has its own copy of the instance variable. In the case
of a static variable, changes will be reflected in
other objects as static variables are common to all
objects of a class.
• We can access instance variables through object
references, and static variables can be accessed
directly using the class name.
• Instance variables are created when an object is
created with the use of the keyword ‘new’ and destroyed
when the object is destroyed. Static variables are
created when the program starts and destroyed when the
program stops.
Syntax: Static and instance variables
class GFG
{
// Static variable
static int a;
// Instance variable
int b;
}
Conclusion
The Important points to remember in the articles are mentioned
below:
• Variables in Java is a data container that saves the
data values during Java program execution.
• There are three types of variables in Java Local
variables, static variables, and instance variables.
FAQs on Variables in Java
Q1. What are variables in Java?
Variables are the containers in Java that can store data values
inside them.
Q2. What are the 3 types of variables in Java?
There are three types of variables in Java are mentioned below:
1. Local Variables
2. Static Variables
3. Instance Variables
Q3. How to declare variables in Java examples?
We can declare variables in java with syntax as mentioned
below:
data_type variable_name;
Example:
// Integer datatype with var1 name
int var1;
Java Data Types
•
Java is statically typed and also a strongly typed language
because, in Java, each type of data (such as integer, character,
hexadecimal, packed decimal, and so forth) is predefined as part
of the programming language and all constants or variables
defined for a given program must be described with one of the
Java data types.
Data Types in Java
Data types in Java are of different sizes and values that can be
stored in the variable that is made as per convenience and
circumstances to cover up all test cases. Java has two categories
in which data types are segregated
1. Primitive Data Type: such as boolean, char, int, short,
byte, long, float, and double
2. Non-Primitive Data Type or Object Data type: such as
String, Array, etc.
Primitive Data Types in Java
Primitive data are only single values and have no special
capabilities. There are 8 primitive data types. They are
depicted below in tabular format below as follows:
Descript Defau Siz Example
Type ion lt e Literals Range of values
twos-
8
compleme
0 bit (none) -128 to 127
nt
s
byte integer
Descript Defau Siz Example
Type ion lt e Literals Range of values
‘a’,
‘\u0041’, characters
Unicode 16
\u000 ‘\101’, representation of
characte bit
0 ‘\\’, ASCII values
r s
‘\’, 0 to 255
char ‘\n’, ‘β’
twos-
16
compleme
0 bit (none) -32,768 to 32,767
nt
s
short integer
twos-
32 -2,147,483,648
compleme -2,-
0 bit to
nt 1,0,1,2
s 2,147,483,647
int intger
-
twos- 9,223,372,036,854,7
64 -2L,-
compleme 75,808
0 bit 1L,0L,1L,
nt to
s 2L
integer 9,223,372,036,854,7
long
75,807
1.23e100f
IEEE 754 32 , -1.23e-
floating 0.0 bit 100f , upto 7 decimal
float point s .3f digits
,3.14F
300d ,
1e1d
Remember: In Java SE 8 and later, you can use the long data
type to represent an unsigned 64-bit long, which has a minimum
value of 0 and a maximum value of 264-1. The Long class also
contains methods like comparing Unsigned, divide Unsigned, etc
to support arithmetic operations for unsigned long.
6. Float Data Type
The float data type is a single-precision 32-bit IEEE 754
floating-point. Use a float (instead of double) if you need to
save memory in large arrays of floating-point numbers. The size
of the float data type is 4 bytes (32 bits).
Syntax:
float floatVar;
Note: Both float and double data types were designed especially
for scientific calculations, where approximation errors are
acceptable. If accuracy is the most prior concern then, it is
recommended not to use these data types and use BigDecimal
class instead.
It is recommended to go through rounding off errors in java.
8. Char Data Type
The char data type is a single 16-bit Unicode character with
the size of 2 bytes (16 bits).
Syntax:
char charVar;
// Class
class GFG {
int i = 89;
// if memory is a constraint
byte b = 4;
// byte b1 = 7888888955;
short s = 56;
// short s1 = 87878787878;
// by default fraction value
// is double in java
double d = 4.355453532;
float f = 4.7333434f;
long l = 12121;
Output
char: G
integer: 89
byte: 4
short: 56
float: 4.7333436
double: 4.355453532
long: 12121
Example:
// Declare String without using new operator
String s = "GeeksforGeeks";
// Declare String using new operator
String s1 = new String("GeeksforGeeks");
2. Class
A class is a user-defined blueprint or prototype from which
objects are created. It represents the set of properties or
methods that are common to all objects of one type. In general,
class declarations can include these components, in order:
1. Modifiers: A class can be public or has default access.
Refer to access specifiers for classes or interfaces in
Java
2. Class name: The name should begin with an initial
letter (capitalized by convention).
3. Superclass(if any): The name of the class’s parent
(superclass), if any, preceded by the keyword extends.
A class can only extend (subclass) one parent.
4. Interfaces(if any): A comma-separated list of
interfaces implemented by the class, if any, preceded
by the keyword implements. A class can implement more
than one interface.
5. Body: The class body is surrounded by braces, { }.
3. Object
An Object is a basic unit of Object-Oriented Programming and
represents real-life entities. A typical Java program creates
many objects, which as you know, interact by invoking methods.
An object consists of :
1. State: It is represented by the attributes of an
object. It also reflects the properties of an object.
2. Behavior: It is represented by the methods of an
object. It also reflects the response of an object to
other objects.
3. Identity: It gives a unique name to an object and
enables one object to interact with other objects.
4. Interface
Like a class, an interface can have methods and variables, but
the methods declared in an interface are by default abstract
(only method signature, no body).
• Interfaces specify what a class must do and not how. It
is the blueprint of the class.
• An Interface is about capabilities like a Player may be
an interface and any class implementing Player must be
able to (or must implement) move(). So it specifies a
set of methods that the class has to implement.
• If a class implements an interface and does not provide
method bodies for all functions specified in the
interface, then the class must be declared abstract.
• A Java library example is Comparator Interface. If a
class implements this interface, then it can be used to
sort a collection.
5. Array
An Array is a group of like-typed variables that are referred
to by a common name. Arrays in Java work differently than they
do in C/C++. The following are some important points about Java
arrays.
• In Java, all arrays are dynamically allocated.
(discussed below)
• Since arrays are objects in Java, we can find their
length using member length. This is different from
C/C++ where we find length using size.
• A Java array variable can also be declared like other
variables with [] after the data type.
• The variables in the array are ordered and each has an
index beginning with 0.
• Java array can also be used as a static field, a local
variable, or a method parameter.
• The size of an array must be specified by an int value
and not long or short.
• The direct superclass of an array type is Object.
• Every array type implements the
interfaces Cloneable and java.io.Serializable.
FAQs of Data Types in Java
1. What are Data Types in Java?
Data types are of different sizes and values that can be stored
in the variable that is made as per convenience and
circumstances to cover up all test cases.
2. What are the 8 Data Types that use in Java?
There are 8 main primitive data types in java as mentioned
below:
• boolean
• byte
• char
• short
• int
• long
• float
• double
3. Which is a Primitive Type in Java?
Primitive data types are the types in java that can store a
single value and do not provide any special capability.
4. Why char uses 2 bytes in Java and what is \u0000?
Char uses 2 bytes in java because it uses the Unicode system
rather than the ASCII system. “\u000” is the lowest range of
the Unicode system.