What is Java
Java is a widely-used programming language for coding web applications. Java is a multi-platform, object-oriented, and network-centric language that can be used as a platform in itself. It is a fast, secure, reliable programming language for coding everything from mobile apps and enterprise software to big data applications and for automation frameoworks.
Java Variables
String - stores text, such as "Hello". String values are surrounded by double quotes
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
boolean - stores values with two states: true or false
Java Data Types
Data types are divided into two groups:
Java Conecepts
There are four main concepts of Java abstraction, encapsulation, polymorphism and inheritance.
Abstraction:
Abstraction is the process to hide implementation details from user and only showing functionality. It only shows essentials things to user not internal process eg: texting or calling on phone. Users are are not aware of of the internal process.
2. Encapsulation:
Simple meaning encapsulation is enclosing something. In Java encapsulation is integrating data varaibles and methods into a single unit.
In encapsulation class variables are hidden from other class and only accessed by methods. To achieve this, you must:
declare class variables/attributes as private
provide public get and set methods to access and update the value of a private variable
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; }}
3. Inheritance:
Inheritance in java is a mechanism in which one object acquires all the properties and behaviour of a parent object. The idea behind the inheritance in java is that to create new classes basesd upon the exsting classes, you can reuse methods of your parent classe also you can add new methods in current class.
For example, let’s say we wanted to create a Student class that inherits from the Person class:
public class Student extends Person { private String major;
public Student(String name, int age, String major) { super(name, age); this.major = major; } public String getMajor() { return major; }}
4.Polymorphism:
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Eg: a person can be a father, son and employee etc. So the same person possesses different behavior in different situations. This is called polymorphism.Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.
5. Object
An object is an instance of a class. It has its own set of properties and can perform actions (methods). To create an object in Java, you use the new keyword, followed by the name of the class and any arguments to the constructor.
For example, to create a Person object, we could do:
Person person1 = new Person("Alice", 30);
This creates a new Person object called person1 with the name "Alice" and age 30.
Interfaces:
In Java, an interface is a reference type that defines a set of abstract methods (methods without a body) and/or constants that a class can implement. Interfaces are similar to classes in that they define a set of methods, but they differ in that they cannot be instantiated directly.
Here’s an example of an interface in Java:
interface Animal { public void eat(); public void sleep();}
In this example, the Animal interface defines two methods, eat() and sleep(), that any class implementing the interface must implement. Note that there are no method bodies - that's because interfaces only define the method signatures, not the implementation details.
A class can implement one or more interfaces by using the implements keyword. Here's an example:
class Dog implements Animal { public void eat() { System.out.println("Dog is eating"); } public void sleep() { System.out.println("Dog is sleeping"); }}
In this example, the Dog class implements the Animal interface by implementing its eat() and sleep() methods. Because Animal is an interface, it cannot be instantiated directly, but it can be implemented by any class that provides an implementation for its abstract methods.