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 :
- Binary Platform Independence.
- 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 Feature||Feature 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.|
Basic Language Constructs of Java Programming Language
Java Basic Language constructs are part of a program that is formed from a combination of one or more tokens which syntactically acceptable and is as per specified rules of programming language.
Java Basic Language Constructs are as follows
- Control Statements
- Class Libraries
Datatype tells the compiler, what kind of data can be assigned to a variable. A datatype can be defined as a type that a variable can have. It can be of two type:
Primitive Data Type
The primitive data type refers to a type which is predefined in a Java language such as boolean, byte, char, short, int, float, long and double etc. They have a fixed range (Default Value) that a variable can store.
User Defined Data Type
User defined data type refers to a type which is defined by the programmer comprising of various built-in data types such as Class, Interface and Arrays.
The following Java Primitive Data Types table shows the following :
- What are the Primitive Data Types in Java ?
- What is the Primitive Data Type Range in Java ?
- How many Bytes of Memory occupy each Primitive Data Type in Java ?
- What is the Default Value of each Primitive Data Type in Java ?
|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|
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.
|Following Java Keywords are added in Java 9 or later|
These are constant values assigned to a variable for example numbers, strings and floating values.
Java Methods refers to the behavior of objects responsible for the changed state of that object. A method is a set of statements which consists a name and invoked by calling it. It performs a specific task and enhances the re-usability of code in a Java program.
Operators can be defined as anything that operates on one or more operands forming an expression and give some output. There are various types of operators such as Assignment operator, Logical operator, Arithmetic operator, increment or decrement operator and Relational operator and bitwise operator.
|Arithmetic||+, -, *, /, %|
|Arithmetic Assignment||+=, -=, *=, /=|
|Logical||!, ||, &&|
|Relational||==, !=, >, <, >=, <=|
|Increment and Decrement||++, --|
|Bitwise||&, |, ^, <<, >>, >>>|
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
- if - else
- do - while
Arrays is a user-defined data type in which elements are stored in a contiguous manner having same data type. Each and every element can be directly accessed by index number.
Java Identifier is a name given to a variable, method, classes and interfaces. The naming is done by considering certain rules. A reserved keyword cannot be used as an identifiers.
Commenting in Java
A comment is a statement which is not considered by a compiler during compilation of the given program. It is very helpful for programmers as it enhances the readability of the program. It can be of three types : Single line comments that limits to a single line whereas multi line comments which limits to two or more lines and Documentation comments which is used for documentation for Java program. Java comments are represented as follows.
- // Single Line Comment
- /* */ Multi Line Comment
- /** */ Documentation Comment
Java Class Libraries
Java Class Libraries are automatically loaded in the program at runtime.
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
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.
- Compile the Java program.
- Run the Java program.
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.
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.