DEV Community

silambarasan rajendran
silambarasan rajendran

Posted on

1

Day-13: Data Types in Java - Class-Specific and Object-Specific Data Types

1). Primitive Data Types:
The Java programming language is statically-typed, which means that all variables must first be declared before they can be used.

example: int grear = 1;

A primitive type is predefined by the language and is named by a reserved keyword.
Like int, byte, char,...

Primitive values do not share state with other primitive values.
Example: int grear = 1;
boolean grear_bike = true;
int total_output = grear + grear_bike
This is not possible.

byte: The byte data type is an 8-bit signed two's complement integer.

  • 128 to 127. The byte data type can be useful for saving memory in large arrays.

short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply .

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer.

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.

float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification.

double: The double data type is a double-precision 64-bit IEEE 754 floating point.

boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

2). Non-Primitive Data Types:
the Java programming language also provides special support for character strings via the java.lang.String class. Enclosing your character string within double quotes will automatically create a new String object; for example, String s = "this is a string";. String objects are immutable, which means that once created, their values cannot be changed. The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such.

Default Values:
The following chart summarizes the default values for the above data types.
Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object) null
boolean false

Datatypes also classified Class and Object Specific:

static String headquarters; - class specific
String bike_colour; - Object specific

public class Honda {

    static String headquarters;  // static variable shared by all instances / class level
    String bike_colour;         // object-specific variable / object level 

    public static void main(String[] args){
        Honda activa = new Honda(); 

        Honda.headquarters = "Japan";  // setting the static variable 
        activa.bike_colour = "Blue";   // setting the instance variable 

        System.out.println("Honda headquarters located at: " + Honda.headquarters);
        System.out.println("Honda bike colour is: " + activa.bike_colour);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output Like this.

Image description

Ref Doc : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

-------------------- End of the Blog -----------------------------

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more