Basics of Java Programming Language
Basics of Java Programming Language
• Java is
– simple, reliable, portable, dynamically adaptable and
powerful language.
– It is a platform-neutral language, It is first programming
language which is not tied to any particular hardware or
operating system.
– It is first application language of the world wide web
– It will also become premier language for general purpose
stand alone applications
Java programming language,
•All source code is first written in plain text files ending with the .java extension. Those
source files are then compiled into .class files by the javac compiler.
•A file does not contain code that is native to your processor; it instead
.class
contains byte codes — the machine language of the Java Virtual Machine (Java
VM).
•The java launcher tool then runs your application with an instance of the Java
Virtual Machine. Because the Java VM is available on many different operating
systems, the same .class files are capable of running on Microsoft Windows, the
Solaris™ Operating System (Solaris OS), Linux, or Mac OS
• An overview of the software development process.
The Java Platform
•A platform is the hardware or software environment in which a program runs, like Microsoft
Windows, Linux, Solaris OS, and Mac OS. Most platforms can be described as a combination of
the operating system and underlying hardware.
•The Java platform differs from most other platforms in that it's a software-only platform that
runs on top of other hardware-based platforms.
Class SampleOne
{
public static void main (string args[])
{
System.out.println (“Java is better than C++.”);
}
}
• Class declaration
– First line
class SampleOne
Keyword Identifier
• Declares a class, which is an object oriented construct
• Java is a true OOP language and therefore, everything
must be placed inside class
• Class is a keyword and declares that a new class
definition follows.
• SampleOne is a java identifier
• Opening Braces
– Every class definition in java begins with opening
brace “{“ and ends with closing brace “}”.
• The Main Line
– The third line defines a method named main, main(),
– Java application can have any number of classes but only
one of them must include a main method to initiate the
execution,
– Java applets will not use main method at all.
– Main method prints text to screen
main() Method is Starting point for
interpreter to begin execution of
application program
• Class names- for all class names the first letter should be 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.
– Example class MyFirstJavaClass
• Method Names- all method names should start with a lower case letter. If several
words are used to form the name of the method, then each inner word’s first
letter should be in upper case.
– Example Public void myMethodName()
• Program file Name – Name of the program file should exactly match the class
name. When saving the file, you should save it using the class name and append
.java to the end of the name.
– Example MyFirstJavaClass is the class name. Then the file name should be saved as
MyFirstJavaClass.java
Examples
/* More java statements
* This program computes addition of two numbers
*/
import java.util.Scanner; //instructs interpreter to load scanner class from package util
class AddNumbers
{
public static void main(String args[])
{
int x,y,z;
System.out.println(“enter two integers to calculate their
sum”);
Scanner in = new Scanner(System.in); // new object in
x = in.nextint(); // executes method from in object
y = in.nextint(); // store values return value to variables
z = x+y;
System.out.println(“sum of entered integers = ” +z); // + concatenates two strings
}
}
• Interface statements
– An interface is like a class but includes a group of methods
declarations.
• Class definition
– Java program may contain multiple class definitions. Classes are
primary and essential elements of a java program. These classes are
used to map the objects of real-world problems.
• Main Methods
– Creates objects of various classes and establishes communication
between them. On reaching end of main the program exits and control
will be transferred to operating system.