8 Basic Data types in Java & Standard Rules for Java Programming

8 Basic Data types in Java & Standard Rules for Java Programming

8 Basic Data types in Java & Standard Rules for Java Programming

In the previous tutorial, I have explained hello world program in detail. The next part is to know what are the different data types in Java. In this tutorial, we will cover the following topics:

Post Content

Data Types in Java

The basic data types in Java include:

  • Integer
  • Character
  • Floating Point
  • Boolean

Basic Data types in JavaLet’s discuss datatypes one by one in detail with its variable size and range…

Integer:

It represents integral arithmetic values. According to the size of data types, it is further classified into:

  • byte
  • short
  • int
  • long
Keyword Memory Size Default  The range of Datatypes Value
byte 1 byte (8 bits) 0 -128….127
short 2 bytes (16 bits) 0 -32,768…..32,767
int 4 bytes (32 bits) 0 Up to 10 digits on the positive and negative end
long 8 bytes (64 bits) 0 Up to 19 digits on the positive and negative end

Character

It is used to represent a single character. 16 bit Unicode character representations are used for character data. The size of a char is 2 bytes (16 bits) and the default value is ‘\u0000’.

Floating Point

They are usually used to represent decimal values. They are of two types:

  • float
  • double
Keyword Memory Size Default The range of Datatypes Value
float 4 bytes (32 bits) 0.0 Up to 10 digits on the positive and negative end
double 8 bytes (64 bits) 0.0 Up to 19 digits on the positive and negative end

In C/C++, we use

float f = 1.5;

However, this is incorrect in Java.

The correct representation requires and additional suffix ‘f’ after the number.

For e.g.:

float f = 1.5f;

Other ways to assign a value of 1.5 to a variable include:

float f = (float)1.5;

Or

double f=1.5;

Boolean

Boolean variables can have only two values, true and false. No other values are permitted. It cannot be cast to other types as well (neither implicitly nor explicitly). The size of Boolean is one bit and the default value is false.

Most of the new programmers have a question…

Why is a string not a primitive datatype in Java?

The string is far different from all primitive datatypes. One of the major difference is its size. We can simply specify the size of the string. It can be of any length. And we can allocate as much memory space as we can for the string.

The string is a major entity and used in many programs for manipulating data. So instead of keeping it as a Data type, it is a class in Java. And there are so many methods to manipulate and to modify the String class object.

So there are two main reasons to make string as a class instead of data types in Java…

  • The string can be of any size(length). This does not hold true for all other data types in Java.
  • Making Java as a class, data manipulation power has emphasized by providing methods for String class object.

This is what makes Java beautiful.

So the data can be the primitive data type or the object of the class.

So we are almost ready to use them in our program. We will use these variables and basic data types throughout our Java programming tutorial.

7 Rules in Java Programming:

All we need now is a set of rules to be followed while writing these programs. The most basic and important rules or guidelines are given below:

  1. Java is a case sensitive language. Hello, hello, heLLo, HellO etc. are all treated as different.
  2. Usually, all the keywords in Java are written in small letters.
  3. Class names are Pascal cased. This means that the class name starts with a capital letter followed by small letters. If the class name comprises of multiple words, the first letter of each word is capital. For e.g.: MyFirstClass.
    This is not really a rule because class names with small first letters are also permissible but it is a convention which is used in all predefined classes and it is advised to use the same naming.
  4. No blank space or special symbols apart from underscore (_) are permissible in class names.
  5. System and String are two major inbuilt classes of Java.
  6. Function names are camel-cased by convention. For e.g.: myFirstFunction()
  7. Program must always be saved with ‘.java’ extension.

There are various other rules and conventions that are often followed related to the readability of program, use of comments to define the purpose of each class, nesting and its limits, consistency, naming, modifiers etc. However, we shall learn these rules as we go on programming further. It is necessary to always remember the above mentioned seven rules.

Do you have any question related to basic data types in Java or guidelines to be followed? Let me know your opinion in the comment section.



Leave a Reply

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