8. Platform Independent:
Java is platform independent: Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into platform specific machines while Java is a write once, run anywhere language. A platform is the hardware or software environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java provides a software-based platform. The Java platform differs from most other platforms in the sense that it is a software-based platform that runs on top of other hardware-based platforms. It has two components:
- Runtime Environment
- API(Application Programming Interface)
Java code can be executed on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc. Java code is compiled by the compiler and converted into bytecode. This bytecode is a platform-independent code because it can be run on multiple platforms, i.e., Write Once and Run Anywhere (WORA).
Distributed: Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating distributed applications. This feature of Java makes us able to access files by calling the methods from any machine on the internet.
Multi-threaded: A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn't occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc.
Dynamic: Java is a dynamic language. It supports the dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.
Application
There are many devices where Java is currently used. Some of them are as follows:
- Desktop Applications such as acrobat reader, media player, antivirus, etc.
- Web Applications such as irctc.co.in, javatpoint.com, etc.
- Enterprise Applications such as banking applications.
- Mobile
- Embedded System
- Smart Card
- Robotics
- Games, etc
2. Language Basics
a. Variables:
There can be three types of variables in java
1. Instance Variables :These are Non-Static
Fields. Individual states of objects are stored in Instance
variables. These are declared without the static keyword. For
number of objects created from a class all objects use same
copy of instance variable.
Example:
import java.io.*;
class Student{
// this instance variable is visible for any child class.
public String name;
// percentage variable is visible in Student class only.
private double percentage;
// The name variable is assigned in the constructor.
public Student (String StudName){
name = StudName;
}
// The percentage variable is assigned a value.
public void setPercentage(double StudPercent){
percentage = StudPercent;
}
// This method prints the Student details.
public void PrintStudentDetails(){
System.out.println("name : " + name );
System.out.println("Percentage :" + percentage);
}
public static void main(String args[]){
Student S1 = new Student("John");
S1.setPercentage(68.89);
S1.PrintStudentDetails();
}
}
2.Class Variables :These are Static Fields.
These are declared using static modifier. There will be only
one copy of each class variable per class for all the objects
created from that class.
import java.io.*;
class Student{
// marks variable is a private static variable
private static int marks;
// Course is a constant
public static final String Course = "IT";
public static void main(String args[]){
marks = 67;
System.out.println(Course +"marks:"+marks);
}
}
3. Local Variables :The syntax for declaring
a local variable is similar to declaring a field (for example,
int count = 0;). These are created when a method, block or constructors
are entered and destroyed when it exits. There are no default
values for local variables and internally these are maintained
at stack level.
public class LocalVar{
public void GetTotal(){
int total = 0;
total = total + 3;
System.out.println("Total is : " + total)
}
public static void main(String args[]){
LocalVar L1 = new LocalVar();
L1.GetTotal();
}
}