Semester III
BCA311 JAVA Programming
Introduction
(Unit-1)
Harshita Mathur
Department of Computer Science
Email :
[email protected] Java Source File Structure
• Java source file should start with a package
declaration. The package declaration is optional
for Java source file. If the package declaration is
default than the package declaration is optional.
• After the package declaration there comes a
section of import declarations. Import
declarations can be zero or more. These import
statements introduce type or static member
names in the source code. Its mandatory to place
all import declarations before actual class
declarations in Java source code.
Java Source File Structure
• After import statements comes the type
declarations statements. There can be any
number of such type declarations, generally
consisting of class , interface etc. The order of
type declarations is not mandatory.
• The Java source file should have one and only one
public class. The class name which is defined as
public should be the name of Java source file along
with .java extention.
A simple java program
//Name of this file will be "Hello.java"
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello Java");
}
}
Some Keywords
• When the main method is declared public, it
means that it can also be used by code outside of
its class, due to which the main method is
declared public.
• The word static used when we want to access a
method without creating its object, as we call the
main method, before creating any class objects.
• The word void indicates that a method does not
return a value. main() is declared as void because
it does not return a value.
• main is a method; this is a starting point of a
Java program.
• String[] args: It is an array where each element
of it is a string, which has been named as "args".
If your Java program is run through the console,
you can pass the input parameter, and main()
method takes it as input.
• System.out.println(); This statement is used to
print text on the screen as output, where the
system is a predefined class, and out is an object
of the PrintWriter class defined in the system.
The method println prints the text on the screen
with a new line. You can also use print() method
instead of println() method.
All Java statement ends with a semicolon.
Compilation and Execution
Compilation and Execution
• Java source code is compiled into Java
bytecode and Java bytecode is interpreted by
the JVM (JAVA Virtual Machine).
• Your Java code may use the code in the Java
library. The JVM executes your code along with
the code in the library.
Compilation and Execution
To execute a Java program is to run the program’s
bytecode. You can execute the bytecode on any
platform with a JVM, which is an interpreter.
It translates the individual instructions in the
bytecode into the target machine language code
one at a time rather than the whole pro- gram as a
single unit.
Each step is executed immediately after it is
translated.
JVM,JRE and JDK
• JVM(Java Virtual Machine) is a program that runs pre
compiled Java programs, which mean JVM
executes .class files (byte-code) and produces output.
• JRE(Java Runtime Environment) is an implementation
of the JVM which actually executes Java programs. It
includes the JVM, core libraries and other additional
components to run applications written in Java. Java
Runtime Environment is a must install on machine in
order to execute pre compiled Java Programs.
• JDK(Java Development Kit)is needed for
developing Java applications. It is a bundle of
software that is used to develop Java based
applications. It includes the JRE, set of API
classes, Java compiler and additional files needed
to write java applications.
• Conclusively, to compile and run Java program
you would need JDK installed, while to run a pre
compiled Java class file (byte-code) you would
need JRE. JRE contains Java interpreter but
not Java compiler.
Compiling and Executing java file on console
• To complie: javac filename.java
• To Execute: java filename