Java from scratch: Variables & Datatypes

Java from scratch: Variables & Datatypes

ยท

5 min read

Data Types

typeSizeRangeDefault
byte1-128 to 1270
short2-32768 to 327670
int4-2147483648 to -21474836470
long8-0
float4+_ 1.4E-45 to +_ 3.4E+38o.of
double8+_ 439E-324 to +_ 1.7E+308ood
char20 to 65535\40000
boolean?true/falsefalse
  • Data is important part in a program, program should hold the data in the memory while the execution of the program.

  • Variables are meant for storing data. In java first we can declare the variable and then store the data into it.

  • Variables will have some data types, the type of data we're going to store into the variable.

Primitive Data Types

  • Basic data types in java that is built in given in the compiler of java.
  1. integral - can have any numerical value without decimal point.

    1. byte

    2. short

    3. int

    4. long

  2. Floating Point - can have numeric value with decimal points.

    1. float

    2. double

  3. char - for storing characters.

  4. boolean - for storing true and false.

    • In Java true means true and false means false, not 1 or 0.

    • How many bytes it takes(1 bit mostly), exactly we can't say, it depends on JVM.

For other languages we've unicode only for english we have ASCII codes. unicode.org Java supports Unicode for supporting all other languages.(It uses char)

Check Size and Range of Data Types

javap java.lang.Integer
import java.lang.*;
class DateSizeRange {
    public static void main(String[] args) {
        System.out.println("Int Min Value"+Integer.MIN_VALUE);
        System.out.println("Int Max Value"+Integer.MAX_VALUE);
        System.out.println("Int Bytes"+Integer.BYTES);
    }
}
Output
Int Min Value-2147483648
Int Max Value2147483647
Int Bytes4

What are Variables?

  • Variables are names given to the data.

  • Or variables are used for storing data.

  • A variable must have some data type.

Example

byte b=5; // 1 byte
int i=175; // 4 bytes
float f=25.3f; // 4 bytes
char c='A'; // 2 bytes
  • We can change the value of the variable later in our program, we can put other data in the variable.

  • Whenever we want to use a variable (ex b) it must be declared first.

  • we can't use a variable unless we initialize it.

Declaration and Inititalization

byte b; // declaration
byte c=5; // initialization

How much memory a variable will occupy?

  • It depends on it's data type.
import java.lang.*;

class Variable {
    public static void main(String[] args) {
        // declare a variable
        byte b;
        // System.out.println("Display b: "+b); throw error
        b=5;
        System.out.println("Display b: "+b); // will display 5
        int a = 145;
        System.out.println(a);
    }
}

Rules for variable names

  1. Case Sensitive
int amount;
int Amount; // different
  1. Contains Alphabets, Numbers, _ or $

  2. Starts with Alphabet, _ or $

  3. Should not be a keyword

Keywords

  • Here's the list of keyWords in java

    • abstract

    • continue

    • for

    • new

    • switch

    • assert

    • default

    • goto

    • package

    • synchronized

    • boolean

    • do

    • if

    • private

    • this

    • break

    • double

    • implements

    • protected

    • throw

    • byte

    • else

    • import

    • public

    • throws

    • case

    • enum

    • instanceof

    • return

    • transient

    • catch

    • extends

    • int

    • short

    • try

    • char

    • final

    • interface

    • static

    • void

    • class

    • finally

    • long

    • strictfp

    • volatile

    • const

    • float

    • native

    • super

    • while

  1. Should not be a class name, if claas is also in use

  2. No limit on lenght of name

  3. Follow Camel Cases

Camel Case

  • first letter of a word should be capital, except of the first letter.
byte rollNumber;

What are literals?

  • Literals are constant values used in a program.
z = 5 * X + 7 * y
// here 5,7 are literals.
int value=25;
// 25 is a literal
double price=153.75;
// 153.75 is literal
area=3.1425 * radius * radius;
// 3.1425 is literal
  • 5 is integer literal.

  • 153.75 is double literal.

  • 3.1425 is double literal.

  • A is char literal.

  • Java is string literal.

Every number having decimal by default is double.

Literals

  • byte - int

  • short - int

  • int - int

  • long - L or I

  • float - F or f

  • double - D or d

  • char - ''

  • boolean - true/false

class Literal {
    public static void main(String[] args) {
        long l = 999999999999999L;
        // int i = 125L; // can't be assigned to int
        System.out.println(l);
    }

}

Check Binary Bits of an Integer

class Checkbinarybits {
    public static void main(String[] args) {
        int x = -5;
        System.out.println(Integer.toBinaryString(x));
    }
}

Float and Character Datatypes in Detail

Floating point

  • Float datatypes don't store decimal actually in the memory but they represent it as a decimal.
163.52
163.52 X 100/100
16352 X 1/100
16352 X 10^-2
16352 - mantisa
10^-2 - exponent
or 16352E-2
  • float takes 4 bytes.

  • It follows IEEE 754 standard(Followed by every electronic device)

  • What about double?

    • Upto 6 digits after decimal, float is fine, but it we want more digits

    • we need to use double.

  • Float - 6-7 digits after decimal

  • Double - 14-15 digits after decimal

Character

  • ASCII standard

  • Every programming language supports Unicodes.

  • For ex:

A - 65
Z - 90
a - 97
z - 122
  • 2 bytes for a character

Did you find this article valuable?

Support KubeKode Blogs by becoming a sponsor. Any amount is appreciated!

ย