Java Programming #2 — Data types and object creation

Sineth Shashintha
4 min readMar 17, 2023

--

Java is a high-level, object-oriented programming language that was created in the mid-1990s by James Gosling and his team at Sun Microsystems. It is a popular language for developing a wide range of applications, including desktop software, web applications, mobile applications, and embedded systems.

Data types in Java

In Java, data types are used to define the type of data that can be stored in a variable, method, or object. Each data type has a predefined set of values and operations that can be performed on it.

We can divide data types into two main types as Primitive data types and non-primitive data types.

Data types in Java

Primitive data types

A primitive data type in Java is a basic data type that is built into the language and is not an object. It is a data type that holds a single value and has no methods or properties associated with it.

There are 8 types of primitive data types.

1. byte: Size — 8 bits

Ex (In java) — byte b = 65;

2. char: In this data type, we can use character values.

Size — 16 bits

Ex (In java) — char character = “a”;

3. short: Size — 16 bits

Ex (In java) — short I = 7;

4. int: Size — 32 bits

Ex (In java) — int h = 17;

5. long: Size — 64 bits

Ex (In java) — long n = 10;

6. floatfloating point

Size — 32 bits

Ex (In java) — float r = 7.5f;

7. doublefloating point

Size — 64 bits

Ex (In java) — double d = 7.34;

8. Boolean binary (true/false):

Size — 1 bits

Ex (In java) — boolean s = true;

Primitive data types in Java

In calculations or operations, we have to use floating numbers to solve our problems. These floating numbers can not be assigned to integer datatypes(byte, char, short, int, long). We have to use ‘float’ or ‘double’ data types to store floating values.

On the other hand, we have to see whether the conditions we use is correct or incorrect. To show them we have to use the words ‘true’ or ‘false’. In these kindsof scenarios, we have to use the ‘boolean’ data type to assign these values.

Specially, we should remember, even though all of above mentioned data types are primitive data types, we can not access all of them by one of the others directly. Let’s see how we can understand it.

Ex:

int a = 2;

long b = 5;

System.out.println(a+b);

This code snippet is correct because both a and b variables are integer

Ex:

int c = 12;

double d = 2.5;

System.out.println(c+d);

This code snippet is incorrect because these variables are in different data types. ‘c’ is an integer and ‘d’ is a floating number.

Non-Primitive data types.

String is also a very popular data type when we use it while programming, specially in Java. String is a pre-build class that we can not change and it can be used as a data type to assign values. In the same way, String is not a primitive data type.

When we declare a variable in String data type, we should remember that we must assign values inside double quotes. In the same way, the word, “String” should be started with an uppercase letter.

Ex:

String name = “John”;

String name1 = “John@1”;

String name2 = “12334”;

Objects in Java

In Java, an object is an instance of a class that encapsulates data and behavior. A class is a blueprint or a template that defines the attributes and methods that an object of that class can have.

If we consider a car as an object, we can identify its properties and behaviors in it as follows.

Properties ==> tires, engine, doors

Behavior ==> drive, reverse, break

How to create an object in Java ??

We can create objects in Java only with the class. In the same way, an object is a conversion of a class.

When we create an object we should use ‘new’ keyword.

If we consider a class as ‘car’, we can create an object of class Car as ‘bmw’. The car model ‘bmw’ also belongs to the class car. We should not create a separate class for it.

Now let’s see an example.

class Car{

Car bmw = new Car();

}

  1. Here, Car is the name of the class that the object belongs to.
  2. Then, we should give the name of the object/variable name.
  3. After that, we use the equation mark to assign it to the.
  4. Next, we use ‘new’ keyword to create the object.
  5. Then, again we use the class name.
  6. Finally, we use brackets(parameter list) and semicolon.

On the other hand, we can create objects in another way by using two lines.

Car bmv;

bmw = new Car();

--

--

Sineth Shashintha

I am a Software Engineering Undergraduate student with the passion of modern technology. Always trying to be happy and make the moments happy respecting others