QR Code for this Page
Java Basics

Java Basics Tutorial

Fundamentals of Java Programming Language

Basics in Java

Java is a simple, portable, distributed, robust, secure, dynamic, architecture neutral, object-oriented programming (OOP) language. And its syntax is like C or C++ Language syntax. Java Language is divided into three categories.

  • Java Standard Edition (JSE)
  • Java Enterprise Edition (JEE)
  • Java Mobile Edition (JME)

Java Standard Edition (JSE)


Java Standard Edition (JSE) is used to create Standalone Applications as well as Graphical User Interface (GUI) Applications.

Java Enterprise Edition (JEE)


Java Enterprise Edition (JEE) is used to create Web Applications as well as Enterprise Applications.

Java Mobile Edition (JME)


Java Mobile Edition (JME) is used to create Mobile Applications.

What is Object-Oriented Programming (OOP) ?

Object-oriented programming (OOP) refers to a type of computer programming (software design) in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure. Object-oriented programming (OOP) is a programming language model in which programs are organized around data, or objects, rather than functions and logic.

Is Java Object-Oriented Programming (OOP) Language ?

Yes, Java is a pure object oriented language. In Java everything is an object, programs are organized around data, or objects, rather than functions and logic.

What is Platform independence or Platform Independent ?

Platform independence is a term that describes a technology (usually a Programming Language or a FrameWork) that you can use to implement things on one machine and use them on another machine without (or with minimal) changes. There are two basic types of Platform Independence :

  1. Binary Platform Independence.
  2. Source Platform Independence

Platform Independence or Platform Independent means that the same program works on any platform (operating system) without needing any modification.

What are the Features of Java Programming Language ?

Java FeatureFeature Description
Object Oriented Java is an object oriented language, everything in Java is an Object.
Platform Independent Unlike other languages, Java applications are typically compiled to bytecode that can run on any Java Virtual Machine (JVM) regardless of the underlying computer architecture. So the Java code is Write Once, Run Anywhere (WORA).
Simple Java is pure object oriented language, it is easy to learn if you know the basic concepts of Object Oriented Programming (OOP).
Secure Java is secure. Java does not have machine code. It has byte code which is interpreted on execution time.
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 Java is known as a portable language because Java code can execute on all major platforms. What's more, once you've compiled your Java source to "byte-code" .class, those files can be used on any Java-supported platform without modification, unlike many other languages, which require compiling "machine code" for each platform.
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.
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.
Distributed Java is designed for the distributed environment of the internet.
Dynamic Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

What are Java Data Types ?

Data Type Range Memory
(in bytes)
Default
Value
boolean true or false 1 bit false
byte -128 to 127 1 0
char any character or 0 to 65535 2 '\u0000'
short -32768 to 32767 2 0
int -2147483648 to 2147483647 4 0
float 1.40129846432481707e-45 to 3.40282346638528860e+38 4 0.0f
long -9223372036854775808 to 9223372036854775807 8 0L
double 4.94065645841246544e-324d to 1.79769313486231570e+308d 8 0.0d

What are Java Keywords ?

Java keywords are reserved words. So you cannot use as identifiers. The keywords const and goto are reserved, even though they are not currently used. Java compiler produce error messages if these are appear in programs.

abstractcontinuefornewswitch
assertdefaultgotopackagesynchronized
booleandoifprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenuminstanceofreturntransient
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfpvolatile
constfloatnativesuperwhile
truefalsenullvar
Following Java Keywords are added in Java 9 or later
exportsmodulerequiresvar

Operators in Java

TypeOperator
Arithmetic+, -, *, /, %
Assignment=
Arithmetic Assignment+=, -=, *=, /=
Logical!, ||, &&
Relational==, !=, >, <, >=, <=
Increment and Decrement++, --
Bitwise&, |, ^, <<, >>, >>>
Separator,
Conditional (Ternary)?:

Java Control Flow Statements

The latest Java version contains important enhancements to improve performance, stability and security of the Java applications that run on your machine. Installing this free update will ensure that your Java applications continue to run safely and efficiently. You can learn more about Java Programming Language from Wikipedia.

Java Decision Making Statements

  1. if
  2. if - else
  3. switch

Java Looping or Iteration Statements

  1. for
  2. while
  3. do - while

Java Branching Statements

  1. break
  2. continue
  3. return

Commenting in Java

  1. //          Single Line Comment
  2. /* */      Multi Line Comment
  3. /** */    Documentation Comment

What are the Software Requirements to Create Java Programs ?

Any one of the following

  • Notepad or any Text Editor
  • NetBeans IDE
  • Eclips IDE

What are the Software Requirements to Run Java Programs ?

  • Java Development Kit (JDK) for Compilation
  • Java Runtime Environment (JRE) for Running Java Programs

What is Java - Basic Syntax ?

Java program is a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what do class, object, methods, and instance variables. The Java program basic syntax is as follows :

  • Object : Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behavior such as wagging their tail, barking, eating. An object is an instance of a class.
  • Class : A class can be defined as a template/blueprint that describes the behavior/state that the object of its type supports.
  • Methods : A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
  • Instance Variables : Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.

How to Run the Java Program ?

Your First Java Program

public class MyFirstJavaProgram
{
	public static void main(String args[])
	{
		System.out.println("WELCOME to HudaTutorials.com");
	}
}

Before going to execute the above program, we have to know the rules of Java program. Rule 1: Java Program name should be the class name. MyFirstJavaProgram in the case of above program with .java extension. So you should type the above program in Notepad of windows OS. And save the file with MyFirstJavaProgram.java in the location C:\HudaTutorials.com\

Before running the above program you should know how to run dos prompt and some basic commands. Go to start menu ⇒ Programs ⇒ Accessories ⇒ click on Command Prompt. You will get the DOS command prompt window. Type MD C:\HudaTutorials.com at the command prompt then press enter.

After type CD C:\HudaTutorials.com press again enter. Your prompt changes to C:\HudaTutorials.com> here we knowing the MD command which is creates a directory and another command is CD it is changing the current directory. Type java –version at your command prompt and then press enter, it shows which Java version is installed on your computer. Then type javac at your command prompt and then press enter. It shows some text that means your environmental variables are working fine. If you want to run your first Java program, you should follow following steps.

  1. Compile the Java program.
  2. Run the Java program.

Compilation

Type javac MyFirstJavaProgram.java then press enter. It does not show any message that means your Java program compiled successfully. If any text showing except your command prompt there are some errors in your program. Check the program then correct the errors then compile again. After successful compilation the javac compiler creates MyFirstJavaProgram.class file in c:\HudaTutorials.com. To check if class file is created type DIR then press enter at your dos prompt.

Running

Type java MyFirstJavaProgram then press enter. It shows the message WELCOME to HudaTutorials.com. You are successfully compiled and ran the program.

Explanation for MyFirstJavaProgram.java

Like C programs Java program execution starts from public static void main(String args[]) method. The System.out.println("WELCOME to HudaTutorials.com"); print the message WELCOME to HudaTutorials.com on the screen.