Professional Documents
Culture Documents
Basic Java Program
Basic Java Program
Basic Java Program
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
Output:
Hello Java
What is constant?
o Constant is a value that cannot be changed after assigning it. Java does not
directly support the constants. For e.g. numeric values in PI. static is a keyword. If
we declare any method as static, it is known as the static method. The core
advantage of the static method is that there is no need to create an object to invoke
the static method. The main() method is executed by the JVM, so it doesn't require
creating an object to invoke the main() method. So, it saves memory.
o void is the return type of the method.
o main represents the starting point of the program.
o String[] args or String args[] is used for command line argument. We will discuss
it in coming section.
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
The documentation section is an important section but optional for a Java program. It
includes basic information about a Java program. The information includes the author's
name, date of creation, version, program name, company name, and description of
the program. It improves the readability of the program. Whatever we write in the
documentation section, the Java compiler ignores the statements during the execution of
the program. To write the statements in the documentation section, we use comments.
The comments may be single-line, multi-line, and documentation comments.
o Single-line Comment: It starts with a pair of forwarding slash (//). For example:
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:
Package Declaration
The package declaration is optional. It is placed just after the documentation section. In
this section, we declare the package name in which the class is placed. Note that there
can be only one package statement in a Java program. It is necessary because a Java
class can be placed in different packages and directories based on the module they are
used. For all these classes package belongs to a single parent directory. We use the
keyword package to declare the package name. For example:
Import Statements
The package contains the many predefined classes and interfaces. If we want to use any
class of a particular package, we need to import that class. The import statement
represents the class stored in the other package. We use the import keyword to import
the class. It is written before the class declaration and after the package statement. We
use the import statement in two ways, either import a specific class or import all classes
of a particular package. In a Java program, we can use multiple import statements. For
example:
1. import java.util.*; //it imports all the class of the java.util package
Interface Section
It is an optional section. We can create an interface in this section if required. We use
the interface keyword to create an interface. An interface is a slightly different from the
class. It contains only constants and method declarations. Another difference is that it
cannot be instantiated. We can use interface in classes by using the implements keyword.
An interface can also be used with other interfaces by using the extends keyword. For
example:
1. interface car
2. {
3. void start();
4. void stop();
5. }
Class Definition
In this section, we define the class. It is vital part of a Java program. Without the class, we
cannot create any Java program. We use the class keyword to define the class. It contains
information about user-defined methods, variables, and constants. Every Java program
has at least one class that contains the main() method. For example:
4. {
5. public static void main(String args[])
6. {
7. //statements
8. }
9. }
What is constant?
o Constant is a value that cannot be changed after assigning it. Java does not
directly support the constants. For e.g. numeric values in PI. static is a keyword. If
we declare any method as static, it is known as the static method. The core
advantage of the static method is that there is no need to create an object to invoke
the static method. The main() method is executed by the JVM, so it doesn't require
creating an object to invoke the main() method. So, it saves memory.
Type of Constants:
Like other programming language, Java also has some constants. In the previous
section, we have discussed, Java constants, how to declare constants. So, in this section,
we will discuss the only types of constants in Java and how to use it.
There are the following types if constants in Java:
1. Numeric Constants
o Integer Constants
o Real Constants
2. Non-numeric Constants
o Character Constants
o String Constants
Numeric Constants
Numeric constants are the constants that contains numerals. It may also have a leading
sign and decimal point.
Integer Constants
A constant that contains digits (0-9) and does not have decimal point is called integer
constants. By default, it is type of int. There are the following three types of integer
constants:
o Decimal Constants: It contains digits between 0 to 9. Note that must not start with
0. For example, 898, 67, 66.
o Octal Constants: It contains digits between 0 to 7 and must begin with 0. For
example, 012, 032, 067.
o Hexadecimal Constants: It contains digits between 0 to 9 and letters a to f (either
in upper or lower case). It must begin with 0X or 0x. For example, 0x23, 0x76,
0X6A, 0XFF.
Real Constants
Numeric constants that have a decimal point are called real or floating-point constants.
By default, the real constants are of double type. We can explicitly mention the type of a
floating-point constant as a float by appending the letter f or F at the end of the constant.
For example, 45f, -0.14f, 5.6F.
o Fractional Form
o Exponential Form
Fractional Form
Exponential Form
It is used to represent a real constant when a number is too small or too large.
For example, 0.00000149 can be represented as 1.49e-6. The part of the number before e
is called mantissa i.e 1.49, whereas, the part after e is called the exponent i.e, 6.
Non-numeric Constants
A constant that does not contain digits is called non-numeric constants. There are the
following two types of non-numeric constants:
Character Constants
A Character constant is a single alphabet, digit or any special symbol enclosed using single
quotes. For example, 'Y', 'd', '6', '#', '&'.
The maximum length of a character constant is 1 character long. It means that we cannot
put more than one character inside single quotation marks.
String Constants
String constants consist of zero or more characters enclosed in double quotes (""). At the
end of the string, the null character i.e '\0' is automatically placed by the compiler. For
example, "hello", , "111",”@june”
The following table denotes the backslash character constants used in Java.
\b Backspace
\f From feed
\n New line
\r Carriage return
\t Horizontal tab
\\ Backslash
\v Vertical tab
\? Question mark
Java Variables:
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.
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
e.g.
There are three scopes for variables in Java: local, instance, class, and method variables.
Local Variables:
Local variables are those that are declared inside of a method .They are not available
outside of the method definition .Local variables can be declared inside the program
blocks that are defined the opening ( { ) and closing braces ( } ). These variables are visible
to the program only.
The Java compiler throws an error if a local variable is not initialised before being used.
The range of local variables is the smallest of all the different variable types.
Example :
public SumExample
Output:
Instance Variables:
1. Within a class, but outside of any methods instance variables are declared. They
are accessible an instance variable can be declared using different access modifiers
available in Java like default, private, public, and protected to all methods and blocks
in the class and are a part of an instance of the class. Instance variables are
accessible inside the same class that declares them.
Example:
where,
<static> implies that all instances of the class share the same static variable
<data_type> is the data type of the variable.
<variable_name> is the name of the variable.
Example :
1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long,
float and double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
The Boolean data type specifies one bit of information, but its "size" can't be defined
precisely.
Example:
The byte data type is used to save memory in large arrays where the memory savings is
most required. It can also be used in place of "int" data type.
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:
Example:
Example:
Example:
1. float f1 = 234.5f
Example:
1. double d1 = 12.3
Example:
1. Class
2. Array
3. Interface
The class represents a group of variables and group of function. Class is a set of object that
having attribute.
Java array is an object which contains elements of a similar data type. Arrays are commonly used
to store multiple values in single variable.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods
in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance
in Java.
1.
2.
1.
2.
•
•
•