Java Data Types Tutorial – Java Tutorial For Beginners

Java Data Types Tutorial – Java Tutorial For Beginners from Coding compiler. Here you will learn about boolean type, integer type, floating point type, character type. Let’s start learning Java now.!

Java Data Types Tutorial For Beginners

Java data types are categorized into boolean, integer, floating point, character types. Here we learn more about each data type.

  • Java Boolean Type
  • Java Integer Type
  • Java Floating Point Type
  • Java Character Type

 

Java Tutorial – Java Boolean Type

 

Java has a Boolean type for logical values. This is the type returned by all relational operators.

value

It can only have one of two possible values, true or false.

Literal value

The Boolean literal has only two logical values: true and false. The values of true and false are not converted to any numeric representation.

True literals in Java are not equal to 1, false literals are not equal to 0. In Java, they can only be assigned to variables declared as boolean.

Boolean class

A Boolean class encapsulates a primitive type Boolean value in an object. An object of type Boolean contains a single field of type boolean.

Boolean classes have methods for converting Boolean values to String and String to Boolean values.

Example

Here is a program that demonstrates the Boolean type:

public class Main {
 public static void main(String args[]) {
   boolean boolVariable;
   boolVariable = false;
   
   System.out.println(“b is ” + boolVariable);
   
   boolVariable = true;
   
   System.out.println(“b is ” + boolVariable);

 }
}

Example 2

The true literal value in Java is not equal to 1, and the false literal value is not equal 0. In Java, they can only be assigned to boolean variables declared as.

public class Main {
 public static void main(String[] argv) {
   boolean b = true;

   int i = b;

 }
}

If you try to compile the program, the compiler will generate the following error message.

Java Tutorial – Java Integer Type

Java byte type

The smallest integer type is the byte. byte Type variables are very useful when using data streams from a network or file.

Byte variables are declared by using the byte keyword. The following declares two byte variables b and c:

byte b, c;

byte Is a signed 8-bit type, ranging from -128 to 127.

The following code creates two byte type variables and assigns values.

Public class Main {
  public static void main(String[] args) {
    byte b1 = 100;
    byte b2 = 20;
   System.out.println( “Value of byte variable b1 is :” + b1);
   System.out.println( “Value of byte variable b1 is :” + b2);
 }
}

The above code produces the following results.

The byte class encapsulates the value of the original type byte in the object. Byte classes provide several ways to convert bytes to strings and strings to bytes.

Java short type

The size of the Java short type is between bytes and integers.

Short is a signed 16-bit type. Short-type variables range from -32,768 to 32,767.

Here are some examples of short variable declarations:

Short s;
short t;

Java int type

When using byte and short values in an expression, they are promoted to int when evaluating the expression.

int It is signed 32-bit type, the range -2,147,483,648to 2,147,483,647.

 

Java long type

When the int type is not large enough, use the Java long type.

Long is a signed 64-bit type and. The long range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 .

To specify a long text, you need to tell the compiler that the literal value is of type long by appending an uppercase or lowercase to the text L. For example, 0x7ffffffffffffffLor 123123123123L.

The following code creates a long type literal and assigns a value to a long type variable.

Public class Main {
  public static void main(String args[]) {
    long l = 0x7ffffffffffffffff;

   System.out.println( “l is ” + l);
 }
}

The output generated by this program is as follows:

Example

Here is a program that uses the long type to store the results.

Public class Main {
    public static void main(String args[]) {
        long result= ( long )Integer.MAX_VALUE * ( long )10;
       System.out.println(result); //21474836470
   
   }
}

The result cannot be stored in an int variable.

The above code produces the following results.

Octal integer (base 8)

Octal values are represented by leading zeros in Java. A valid value of 09 will generate an error from the compiler because 9 is outside the octal 0 to 7 range.

Public class Main {

 Public static void main(String[] args) {
    int i = 010;

   System.out.println(i);
 }
}

Output:

Hexadecimal integer (base 16)

Hexadecimal matches the modulo 8-word size (eg 8, 16, 32 and 64 bits). You represent a hexadecimal constant with a leading zero-x (0x or 0X).

Hexadecimal numbers range from 0 to 15, so A to F (or a to f) instead of 10 to 15.

Integer constants can always be assigned to a long variable. You can also assign an integer to char as long as it is in range.

Public class Main{
  public static void main(String[] argv){
    int f = 0XFFFFF;

   System.out.println(f); //1048575
   
 }
}

The above code produces the following results.

 

Java Tutorial – Java Floating Point Type

 

Float type

float The type represents a single precision number.

float Type variables are useful when you need a fractional component. Here are some examples of floating variable declarations:

float high, low;

Java floating point value, size and literal value

The float is 32 bits wide and its range is approximately1.4e-045 to 3.4e + 038.

Floating-point literals in Java default to double precision. To specify a floating-point literal, you must append an F to the constant f.

The following code shows how to declare a floating-point literal.

Public class Main {
    public static void main(String args[]) {
        float d = 3.14159F;
       System.out.print(d); //3.14159    
    }
}

The above code produces the following results.

 

Java double type

The Java double type represents a double precision number.

Double is 64 bits wide and ranges from approximately 4.9e-324 to 1.8e + 308.

Here is a program that uses two variables to calculate the area of a circle:

Public class Main {
  public static void main(String args[]) {
    double pi, r, a;

   r = 10.8888; // radius of circle
    pi = 3.1415926; // pi, approximately
    a = pi * r * r;

   System.out.println( “Area of circle is ” + a);
 }
}

 

Example

Double-precision numbers have fractional values with fractional parts. They can be expressed in standard or scientific notation. The standard symbol consists of an integer number followed by a decimal point followed by a decimal point. For 2.0example, , 3.14159and 0.6667.

Public class Main {
    public static void main(String args[]) {
        double d = 3.14159;
       System.out.print(d); //3.14159    
    }
}

The above code produces the following results.

Example 2

You can explicitly specify a double literal by appending D or d.

Public class Main {
    public static void main(String args[]) {
        double d = 3.14159D;
       System.out.print(d); //3.14159    
    }
}

The above code produces the following results.

Scientific notation

Scientific notation uses standard notation, floating point number plus the suffix power of the specified a multiplied by the number 10. The index is indicated by E or e, followed by a decimal number, which can be positive or negative. For example, 6.02E23, 314159E-05and 4e + 100.

Public class Main {
  public static void main(String[] argv) {
    double d1 = 6.022E23;
    double d2 = 314159E-05;
    double d3 = 2e+100;

   System.out.println( “d1 is ” + d1);
   System.out.println( “d2 is ” + d2);
   System.out.println( “d3 is ” + d3);
 }

}

The output generated by this program is as follows:

Double value constant

Java’s floating point calculations can return + infinity, -infinity, +0.0, -0.0andNaN

Dividing a positive number by 0.0 will report + infinity. For example, the System.out.println(1.0/0.0);output is infinite.

Public class Main{
  public static void main(String[] args) {
   System.out.println(1.0/0.0);
 }

}

The above code produces the following results.

Double infinity

Divide the negative number by the 0.0 output – infinity. For example, System.out.println(-1.0/0.0); Output – infinity.

Public class Main{
  public static void main(String[] args) {
   System.out.println(-1.0/0.0);
 }

}

Output:

Double NaN

Will be 0.0divided by the 0.0return NaN. The square root of a negative number is NaN. For example, System.out.println(0.0/0.0)and System.out.println(Math.sqrt(-1.0))output NaN.

Divide the positive number by + infinity output + 0.0. For example, System.out.println(1.0 /(1.0/0.0)); output +0.0.

Divide the negative number by + infinity to output -0.0. For example, System.out.println(-1.0 /(1.0/0.0)); output -0.0.

Public class Main {
  public static void main(String[] args) {
   Double d1 = new Double(+0.0);
   System.out.println(d1.doubleValue());

   Double d2 = new Double(-0.0);
   System.out.println(d2.doubleValue());
   System.out.println(d1.equals(d2));
   System.out.println(+0.0 == -0.0);

   
 }
}

 

Java Tutorial – Java Characters

 

In Java, char stores characters. Java uses Unicode to represent characters. Unicode can represent all the characters found in all human languages.

Java char is a 16-bit type.

The range of characters is 0to 65,536. There are no negative characters.

Char text

Characters in Java are indexes of the Unicode character set. Characters are represented in a pair of single quotes. For example, ‘a’, ‘z’ and ‘@’.

Here is a program that demonstrates the char variable:

Public class Main {
  public static void main(String args[]) {
    char ch1, ch2;

   Ch1 = 88; // code for X

   ch2 = ‘ Y’ ;

   System.out.print( “ch1 and ch2: ” );
   System.out.println(ch1 + ” ” + ch2); //ch1 and ch2: XY
  }
}

The above code produces the following results.

ch1The value 88 is assigned, which is Xthe ASCII (and Unicode) value corresponding to the letter.

Example

charType values can be used as integer types and you can perform arithmetic operations.

Public class Main {
  public static void main(String args[]) {
    char ch1;

   Ch1 = ‘ X’ ;
   System.out.println( “ch1 contains ” +ch1); //ch1 contains X

   ch1 = ( char )(ch1 + 1); // increment ch1
    System.out.println( “ch1 is now ” + ch1); / /ch1 is now Y
  }
}

Example 2

The following code shows that we can assign Java character types to non-alphabetic characters.

Public class Main {
  public static void main(String[] argv) {
    char ch = ‘ a’ ;

   System.out.println( “ch is ” + ch); //ch is a
    ch = ‘ @’ ;

   System.out.println( “ch is ” + ch); //ch is @
    ch = ‘ #’ ;

   System.out.println( “ch is ” + ch); //ch is #
    ch = ‘ $’ ;

   System.out.println( “ch is ” + ch); //ch is $
    ch = ‘ %’ ;

   System.out.println( “ch is ” + ch); //ch is %
  }
}

The above code produces the following results.

Example 3

The following code stores the Unicode value in a char variable. The unicode text uses the \\uxxxxformat.

Public class Main {
  public static void main(String[] args) {
    int x = 75;
    char y = ( char ) x;
    char half = “\u00AB” ;
   System.out.println( “y is ” + y + ” and half is ” + half);

 }
}

 

Example 4

The escape sequence is used to enter characters that cannot be entered directly.

The syntax for escape character values:

‘\”Used for single quote characters. ‘\n’Used for line breaks.

For octal symbols, use a backslash followed by three digits. For example, ‘\141’the letter ‘a’.

For hexadecimal, you enter the backslash -u( \u) and then exactly four hexadecimal digits. For example, ‘\u0061’” is ISO-Latin-1 ‘a’ because the top byte is zero. It ‘\ua432’is a Japanese Katakana character.

Public class Main {
  public static void main(String[] argv) {
    char ch = ‘ \” ;

   System.out.println( “ch is ” + ch); //ch is ‘

 }
}

The character is a simple wrapper.

 

Escape from the list of values

The following table shows the character escape sequences.

Escape sequence description
\ddd Octal character (ddd)
\uxxxx Hexadecimal Unicode character (xxxx)
\’ apostrophe
\” Double quotes
\\ Backslash
\r Enter
\n Newline
\f Form feed
\t Escape character
\b backspace

Related Java Tutorials & Interview Questions

Related Java Tutorials & Interview Questions
Introduction to Java Programming 21 Aricent Java Interview Questions
Java Keywords Tutorial For Beginners 53 Accenture Java Interview Questions
Java Performance Tuning Tips 399 Core Java Interview Questions
Core Java Multiple Choice Questions 581 Advanced Java Interview Questions
60 Java Multiple Choice Questions And Answers More Java Tutorials To Come..

Leave a Reply

Your email address will not be published. Required fields are marked *