Java 1 - Basics of Java

Java

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java is widely used for developing desktop, web, and mobile applications, as well as for building games, embedded systems, and big data technologies.

OR

Java is a general-purpose, class-based, object-oriented computer programming language that is specifically designed to have as few implementation dependencies as possible. Java was designed to be portable, which means that Java code can run on any device that has a Java Virtual Machine (JVM) installed. This makes Java a popular choice for developing cross-platform applications, including mobile apps, web applications, and desktop software. Java has a large and active developer community, which contributes to its continued evolution and the creation of a vast library of pre-written code, called the Java API, which makes it easier for developers to build new applications quickly.

Characteristics of the Java programming language:


Object-Oriented: Java is an object-oriented programming language, meaning that it allows developers to model real-world objects and their behavior in software.


Platform Independent: Java code can run on any device that has a Java Virtual Machine (JVM) installed, making it platform independent.


Simple and Secure: Java was designed to be easy to learn and use, with a focus on security. Java's memory management and automatic garbage collection help prevent common programming errors and ensure the security of the code.


Multithreaded: Java supports multithreading, which allows for concurrent execution of multiple threads within a single program. This makes it easy to create and manage multi-threaded applications.


Dynamic: Java is a dynamically-typed language, meaning that the data type of a variable can change at runtime.


Portable: Java code can run on any platform that has a JVM installed, ensuring that the same code can run on multiple platforms with little or no modification.


Robust: Java includes features such as exception handling and type checking that help make the language more robust and less prone to errors.


Large Community: Java has a large and active community of developers, with many resources available for learning and troubleshooting 


Data Types

In Java, there are two categories of data types: 

1. Primitive data types 

2. Reference data type or Non Primitive data type


Primitive data types:

byte: 8-bit signed integer value.

short: 16-bit signed integer value.

int: 32-bit signed integer value.

long: 64-bit signed integer value.

float: 32-bit floating point value.

double: 64-bit floating point value.

char: 16-bit Unicode character.

boolean: represents a binary value, either true or false.


Reference data types:


Object: the root class of all classes in Java, can be used to store objects of any type.

String: used to store a sequence of characters, often used to represent text.

Array: a collection of homogeneous elements of the same data type.

Classes: user-defined data types that can store multiple values of different data types.

It's also worth noting that all reference data types in Java are objects, which means they are stored on the heap, and are accessed through references. Primitive data types, on the other hand, are stored on the stack and have a direct value.

What is class ?

A class in Java is a blueprint for creating objects, providing structure for the data and behavior of those objects. Classes are the building blocks of object-oriented programming, and they can be used to define custom data types in Java.


A class is defined using the class keyword, followed by the name of the class. The body of the class is contained within curly braces, {}. A class can contain fields (also known as variables or attributes), methods (also known as functions), and constructors (special methods used to create instances of the class).


Here's an example of a simple class in Java:


public class Person {

  // fields or attributes

  private String name;

  private int age;


  // constructors

  public Person() {}

  public Person(String name, int age) {

    this.name = name;

    this.age = age;

  }


  // methods

  public void sleep() {

    System.out.println("....");

  }

  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;

  }

}

In this example, the Person class has two fields, name and age, and several methods that allow you to set and retrieve the values of those fields, as well as perform other actions. To use this class, you would create an instance of the class, which is also known as an object. For example:


Person m1 = new Person("Buddy", 3);

m1.sleep(); // Output: ...

Classes are an essential part of Java programming, and they provide a way to encapsulate data and behavior in a single, reusable entity.

What is an object?

An object in Java is an instance of a class, created at runtime. Objects have their own individual state and behavior, and they can be used to represent real-world entities or abstract concepts.


An object is created by calling a constructor of a class. The constructor is a special method that has the same name as the class and is used to initialize the object's state.


For example, if you have a class Person, you can create an object of that class as follows:


Person m1 = new Person("Buddy", 3);

Once you have created an object, you can access its fields and methods using the dot notation. For example:


m1.sleep(); // calls the sleep() method of the Person object

Objects can also interact with each other by sending messages to each other in the form of method calls. This is one of the key concepts of object-oriented programming, and it allows you to model complex systems by composing objects that interact with each other.


In Java, all objects are stored on the heap, and they are accessed through references. This means that when you create an object, you are not storing the actual object in a variable, but rather a reference to the object on the heap. When you pass an object to a method, you are passing the reference to the object, not the actual object itself.


Comments