• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About Us
    • About CSEStack:
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • Campus Ambassador
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • C/C++
    • Python
    • Java
    • HTML CSS
    • SQL
  • CSE Subject
    • Compiler Design
    • Computer Network
    • COA
    • Data Structure
    • DBMS
    • Operating System
    • Theory of Automata
    • Web Technology
  • Linux
  • Trend
    • Artificial Intelligence (AI)
    • Big Data
    • Cloud Computing
    • Machine Learning (ML)
  • GATE CSE 2020
    • Self Study Plan
    • Complete Syllabus
    • FREE Test Series
    • Topper Interview (AIR 15)
    • Recommended Books by Topper
  • Career
    • Placement Interview
    • Jobs
    • Aptitude
    • Quiz
  • Material
    • Recommended Books
    • Software Installation
  • Contribute to Us
    • Write for Us
    • Submit Source Code or Program
    • Share Interview Experience
  • Tools

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

Heena Rajpal/19 Jun, 17/5277/0
CodeJAVA

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 following topics:

Post Content

  • Data Types in Java
  • Why is String not a primitive datatype in Java?
  • 7 Rules of writing Java Program

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  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 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 though out 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.


« Writing Hello World Program String Handling in Java »

CodeJava

Related Posts

C / C++Code

Nested Switch Statements in C Programming with Real Life Example

C / C++Code

Split String in C programming using strtok_r() | Parsing CSV File

C / C++CodeJAVAPython

[Explained in Detail] if else nested Programming Example in C, Java and Python

Heena Rajpal
Heena Rajpal is pursuing Computer Science Engineering from Indore. She has a knack for writing and an inquisitive outlook towards Computer Science fields like Database Management Systems, Object Oriented Programming and languages like C, C++, JAVA, Python, HTML etc.

Leave a Reply Cancel reply

Interview Experiences

AccentureAgriChain Akamai Amazon Amdocs American Express Attra Infotech BARC Barclays BlockGrain BYJUS Cisco Cognizant Coupon Dunia Credit Suisse DE Shaw Druva Experis IT Eze Software Factset Fiorano General Electric Incture Technologies Infosys Kasmo Cloud Microsoft MindTree Mu Sigma Numerify Opteamix Oracle Persistent Pole to Win Qualcomm Reliance Riverbed Syntel TCS Tech Mahindra Teradata Terralogic Virtusa Wipro

Interview Questions



You can share your interview experience.

Don’t Miss !

Latest Articles

Cisco Online Test Pattern and Interview Questions for Freshers


[7 Best Tips] How to Make Daily Study Timetable and Stick to It?


Persistent Written Test and Technical Coding Questions [Paper Pattern]


10 Top Website Design Tips for Small Business to Boost Up Your Sales


Main Difference Between remove del and pop in Python List


Tech Mahindra Placement (Online Test | Essay Writing | Interview Questions)


Importance of Career Exploration for High-School Students | Why?


Favorite Topic

AI algorithm array bigdata bit manipulation career Code Computer Network cpp data analytics database data scientist Data Structure db DBMS difference between Django education GATE GATE Topper Interview HTML ibm IBM ISDL interview IP address Java JavaScript Jobs Linked List linux linux cmd nit OOPs Concept os Programming Python python list Qualcomm SOAP sorting stack string vim webbrowser web development

Contribution to Community

  • Contribute to CSEstack Portal
  • Submit Your Source Code or Program
  • Share Your Interview Experience
  • CSEStack Leadership Board
  • Campus Ambassador Program by CSEstack

About CSEstack Portal

  • About CSEStack:
  • Contact Us
  • CSEStack Campus Ambassador
  • Recommended Books by Expert

© 2019 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About Us
    • About CSEStack:
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • Campus Ambassador
  • LeaderBoard