Java Definition I

Introduction

Java Definitions I

Contents

  1. Variables in Java
  2. Main method in Java
  3. Package in Java
  4. Method in Java
  5. Primitive Types in Java

The Java language project was initiated in 1991 by James Gosling and his colleagues. In the beginning, the language was called “Oak.” Later the project was renamed “Java” as a reference to Java coffee. For this reason, the language’s logo is a cup of coffee.

1. Variables in Java

Variables are a way to store information in our computer. Variables that we define in a program can be accessed by a name we give them, and the computer does the hard work of figuring out where they get stored in the computer’s random access memory(RAM).

An object knows things and does things.

  • Things an object knows about itself are called instance variables. They represent the state of an object.
  • Things an object does are called methods They represent the behavior of an object.
  1. Instance variables are declared inside a class but not within a method.
  2. Local variables are declared within a method.
  3. Local variables Must be initialized before use!
    • From Head First Java

2. Main method in Java

The Main method that Java looks for when running a program. It is the entry point of any java code.

Hello.java file

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
Hello World

Without main method

public class Hello {
    public void method() {
        System.out.println("Hello World");
    }
}
error: can't find main(String[]) method in class: Hello

3. Package in Java

A package is a namespace that organizes a set of related classes and interfaces.

Main Class in the named sample package. sample/Main.java

package sample;

public class Main {
    public static void main(String[] args) {
        System.out.println("Main.java is in sample package");
    }
}

Compile and Execute in command line

$ javac sample/*.java
$ java sample.Main
Main.java is in sample package

4. Method in Java

Java method is a collection of statements that are grouped together to perform an operation. 

public class Method {
    public static void main(String[] args) {

        addTwo(true, 3);
        addTwo(false, 3);
    }

    public static int addTwo(boolean calculate, int num) {
        if (calculate) {
            System.out.println("+2: " + (num + 2));
            return num + 2;
        } else {
            System.out.println("+0: " + num);
            return num;
        }
    }
}
+2: 5
+0: 3

5. Primitive Types in Java

Java Primitive types are the most basic data types.

The eight primitive data types in Java are byte, short, int, long, float, double, boolean, and char.

each primitive type occupies a different amount of memory.

byte, short, int, long, float, double

public class Primitive {
    public static void main(String[] args) {

        // byte
        System.out.println("Byte Minimum: " + Byte.MIN_VALUE);
        System.out.println("Byte Maximum: " + Byte.MAX_VALUE);

        // short
        System.out.println("Short Minimum: " + Short.MIN_VALUE);
        System.out.println("Short Maximum: " + Short.MAX_VALUE);

        // integer
        System.out.println("Integer Minimum: " + Integer.MIN_VALUE);
        System.out.println("Integer Maximum: " + Integer.MAX_VALUE);

        // ;ong
        System.out.println("Long Minimum: " + Long.MIN_VALUE);
        System.out.println("Long Maximum: " + Long.MAX_VALUE);


        // float
        System.out.println("float Minimum: " + Float.MIN_VALUE);
        System.out.println("float Maximum: " + Float.MAX_VALUE);

        // double
        System.out.println("Double Minimum: " + Double.MIN_VALUE);
        System.out.println("Double Maximum: " + Double.MAX_VALUE);
    }
}
Byte Minimum: -128
Byte Maximum: 127
Short Minimum: -32768
Short Maximum: 32767
Integer Minimum: -2147483648
Integer Maximum: 2147483647
Long Minimum: -9223372036854775808
Long Maximum: 9223372036854775807
float Minimum: 1.4E-45
float Maximum: 3.4028235E38
Double Minimum: 4.9E-324
Double Maximum: 1.7976931348623157E308
  • A byte occupies 8 bits.
  • A short occupies 16bits
  • A int occupies 32 bits
  • A long occupies 64bits
  • Single precision number: float
    • A float occupies 32bits
  • Double precision number: double
    • A double occupies 64bits

char, boolean

A char occupies two bytes of memory, or 16 bits. The reason it is not just a single byte is that it allows you to store Unicode characters.

  • Unicode is an international encoding standard for use with different languages and scripts, by which each letter, digit, or symbol is assigned a unique numeric value that applies across different platforms and programs.
public class Unicode {
    public static void main(String[] args) {

        char myChar = 'D';
        char myUnicodeChar = '\u0044';
        System.out.println(myChar);
        System.out.println(myUnicodeChar);
    }
}
D
D

A boolean value allows for two choices True or False, 1 or 0. In Java terms we have a boolean primitive type and it can be set to two values only. true or false.

public class Boolean {
    public static void main(String[] args) {

        boolean trueValue = true;
        boolean falseValue = false;

        if(!falseValue){
            System.out.println(trueValue);
        }
    }
}
true

Leave a Reply

Your email address will not be published.

ANOTE.DEV