Fundamentals of Java Programming Language
Java is a high-level, versatile programming language used for developing large-scale applications.
2025-02-17T07:35:26.711Z Back to posts
Fundamentals of Java Programming Language
======================================================
Java is an object-oriented programming language that has become one of the most popular languages in the world. It’s a high-level, versatile language used for developing large-scale applications, including Android apps, web applications, and enterprise software.
What is Java?
History of Java
Java was created by James Gosling and his team at Sun Microsystems (now owned by Oracle) in the mid-1990s. The first version of Java, known as Oak, was developed in 1991, but it wasn’t until the release of Java 1.0 in 1995 that the language gained popularity.
Key Features
Java’s key features make it an attractive choice for developers:
- Platform independence: Java code can run on any platform that has a Java Virtual Machine (JVM) installed.
- Object-oriented: Java supports encapsulation, inheritance, and polymorphism, making it easier to write reusable code.
- Simple syntax: Java’s syntax is easy to learn and use, especially for beginners.
Basic Syntax
Variables and Data Types
Java has eight primitive data types:
Data Type | Description |
---|---|
byte | 8-bit signed integer |
short | 16-bit signed integer |
int | 32-bit signed integer |
long | 64-bit signed integer |
float | 32-bit floating-point number |
double | 64-bit floating-point number |
boolean | true or false value |
char | single character |
int x = 10; // Declare and initialize an integer variable
Operators
Java has various operators for performing arithmetic, comparison, logical, and assignment operations:
- Arithmetic operators:
+
,-
,*
,/
,%
- Comparison operators:
==
,!=
,>
,<
,>=
,<=
- Logical operators:
&&
(and),||
(or) - Assignment operator:
=
int x = 5;
int y = 10;
System.out.println(x + y); // Output: 15
Control Structures
Java’s control structures are used to control the flow of a program:
- Conditional statements:
- If-else statements (
if
,else
) - Switch statements (
switch
) - Loops:
- For loops (
for
) - While loops (
while
)
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
} else {
System.out.println("x is less than or equal to 5");
}
Classes and Objects
Classes
A class in Java is a blueprint for creating objects. It defines the properties and behavior of an object:
public class Person {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Objects
An object in Java is an instance of a class. It has its own set of attributes (data) and methods (functions):
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("John");
person.setAge(30);
System.out.println(person.getName()); // Output: John
System.out.println(person.getAge()); // Output: 30
}
}
Inheritance and Polymorphism
Inheritance
Inheritance in Java allows one class to inherit properties and behavior from another class:
public class Animal {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class Dog extends Animal {
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
Polymorphism
Polymorphism in Java allows objects of different classes to be treated as if they were of the same class:
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.setName("Max");
((Dog)animal).setAge(2);
System.out.println(animal.getName()); // Output: Max
System.out.println(((Dog)animal).getAge()); // Output: 2
}
}
This concludes the basics of Java programming language. With this knowledge, you can start building your own Java programs and explore more advanced concepts in object-oriented programming.
Additional Resources
For further learning, check out the official Java documentation and online resources like Codecademy’s Java course or Udemy’s Java courses.