Java Notes
Java Notes

Java Notes

Tags
Java
Published
September 14, 2022
Notes for Java

Java Data-types

Primitive Data Type

A primitive data type specifies the size and type of variable values, and it has no additional methods.

byte

  • The byte data type can have values from 128 to 127 (8-bit signed two's complement integer).
  • If it's certain that the value of a variable will be within -128 to 127, then it is used instead of int to save memory.
  • Default value: 0

short

  • The short data type in Java can have values from 32768 to 32767 (16-bit signed two's complement integer).
  • If it's certain that the value of a variable will be within -32768 and 32767, then it is used instead of other integer data types (int, long).
  • Default value: 0

int

  • The int data type can have values from 231 to 2311 (32-bit signed two's complement integer).
  • If you are using Java 8 or later, you can use an unsigned 32-bit integer. This will have a minimum value of 0 and a maximum value of 21. To learn more, visit How to use the unsigned integer in java 8? 32
  • Default value: 0

long

  • The long data type can have values from 263 to 2631 (64-bit signed two's complement integer).
  • If you are using Java 8 or later, you can use an unsigned 64-bit integer with a minimum value of 0 and a maximum value of 2641.
  • Default value: 0

float

  • The float data type is a single-precision 32-bit floating-point. Learn more about single-precision and double-precision floating-point if you are interested.
  • It should never be used for precise values such as currency.
  • Default value: 0.0 (0.0f)

double

  • The double data type is a double-precision 64-bit floating-point.
  • It should never be used for precise values such as currency.
  • Default value: 0.0 (0.0d)

char

  • It's a 16-bit Unicode character.
  • The minimum value of the char data type is '\u0000' (0) and the maximum value of the is '\uffff'.
  • Default value: '\u0000'

boolean

  • The boolean data type has two possible values, either true or false.
  • Default value: false.
  • They are usually used for true/false conditions.

Non-primitive Data Type

String

Print Line

System.out.println() can print to the console:
  • System is a class from the core library provided by Java
  • out is an object that controls the output
  • println() is a method associated with that object that receives a single argument
System.out.println("Hello, world!"); // Output: Hello, world!

Comments

Comments are bits of text that are ignored by the compiler. They are used to increase the readability of a program.
  • Single line comments are created by using //.
  • Multi-line comments are created by starting with /* and ending with /.
// Single line comment! /* Multi Line Comment */

Main Method

In Java, every application must contain a main() method, which is the entry point for the application. All other methods are invoked from the main() method.
The signature of the method is public static void main(String[] args) { }. It accepts a single argument: an array of elements of type String.
public class Person { public static void main(String[] args) { System.out.println("Hello, world!"); } }

Classes

A class represents a single concept.
A Java program must have one class whose name is the same as the program filename.
In the example, the Person class must be declared in a program file named Person.java.
public class Person { public static void main(String[] args) { System.out.println("Hello World"); } }

Naming variable

A variable starts with a valid letter, or a $, or a _. No other symbols or numbers can begin a variable name.

Interface

An interface in Java is a blueprint of a class. It has static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.

Use of Interface

  • There are mainly three reasons to use interface. They are given below.
  • It is used to achieve abstraction.By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.

Layout Manager

The LayoutManagers are used to arrange components in a particular manner.
LayoutManager is an interface that is implemented by all the classes of layout managers.
There are the following classes that represent the layout managers:
  • Java BorderLayout - The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the default layout of a frame or window.
  • Java GridLayout - The Java GridLayout class is used to arrange the components in a rectangular grid. One component is displayed in each rectangle.
  • Java FlowLayout - The Java FlowLayout class is used to arrange the components in a line, one after another (in a flow). It is the default layout of the applet or panel.
  • Java BoxLayout - The Java BoxLayout class is used to arrange the components either vertically or horizontally.