Java Tutorials - Lesson 2: Primitive Data Types and Variables (Integer, double, float, and more)
Java is much like most other programming languages in that you can have variables. The idea behind a variable is to hold data that can change. Java has many types of primitive data types to hold different types of data based what the data is to represent.
Primitive Data Types
A primitive data type is a type of data that is essentially hard coded into the language and is represented with a reserved word(a word that has specific meaning and functionality in the language) in Java. Primitive data types are one of the building blocks of Java.
There are 8 primitive data types in Java:
Integer Types
- boolean-This data type holds true or false only. It is represented logically by 1 bit (0 for false, 1 for true).
- byte-This data type holds values between -128 to 127(integer only). It is represented by 8 bits in memory(2's complement form).
- short-This data type holds values between -32,768 and 32,767. It is represented by 16 bits in memory(2's complement form).
- int-This data type holds values between -2,147,483,648 and 2,147,483,647. It is represented by 32 bits in memory(2's complement form).
- long-This data type holds values between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. It is represented by 32 bits in memory(2's complement form).
- char-This data type holds a 16 bit unicode character.
Decimal Types
- float-This data type is represented by 32 bits in memory. It can store decimal numbers typically with 7 significant figures. (example: 45.3453) This data type should not be used with extremely precise applications.
- double-This data type is represented by 64 bits in memory. It can store decimal numbers typically with 15 significant figures. (example: 45.3453) This data type should not be used with extremely precise applications.
Creating Variables
Variables, once declared, can be used throughout your program.
A variable is easy to declare and give a value. Several examples are below of creating a variable.
boolean myBool = true;
int myInt = 34342;
double myDouble = 349634.4564
The default value is the value that is automatically assigned if you do not assign a value. Values are always initialized to 0 or null. Even so, it is recommended that you ALWAYS assign a value to your variables as you declare them.
Declaring Constants
A constant is a value that does not change. An example of this is the value PI, 3.14159... To declare a constant, use the final keyword.
Example:
final double PI = 3.14159;
The value of constants cannot be changed during program run! If you attempt to do so, the program will not compile, or if it does, the program will stop running and give you an error.
Isn't that much easier than typing 3.14159 several times throughout your program!
Naming Variables
Now that you know how to declare and assign a variable, let's go into detail about what variable names are allowed.
- The variable cannot be a reserved word. A list of all reserved words can be found below.
- The variable can contain any combination of letters, numbers,
-
What's the Best Free Learn to Type Software?
| By Willow_Sidhe | in Software
In this article, I examine the few REAL free learn to type software programs so you can make the best decision befo...
-
Database Table Data Types
| By Chrys | in Programming
In this part of the series we look at database Data Types....
-
Type 1 Diabetes
| By Master | in Diseases & Conditions
Type 1 Diabetes, also known as insulin-dependent diabetes or juvenile diabetes is a chronic (life-long) diseases. T...
-
Type 2 Diabetes
| By Master | in Diseases & Conditions
Type 2 Diabetes is the most common variety of the disease, accounting for as much as 90 percent of all cases of dia...
-
How to Hack or Crack a Windows XP Administrator Password | By pkumarb89 | in Programming
This is provided only for educational purpose it is a simple way to Recover, Hack or Crack the Window XP Administra...
-
What Is Hacking? | By faris_jayz | in Programming
It is not that difficult to hack into a computer system. The most popular definition for hacking is the act of obta...
-
How do I enable JavaScript in browser? | By myjobs | in Programming
way to enable the javascript in different types of browser....
-
iPhone Core Data Tutorial Part 3.2 | By eh9212 | in Programming
How to use To-Many relationships in core data...
-
How to Set Up a Local Development Environment in Ubuntu | By havard | in Programming
Learn how to set up a local development environment in Ubuntu that set up your websites on the fly....
-
Learning Linux -- The basics | By koolmelee1 | in Software
Have you wanted to be good at the linux command prompt, but never bothered learning how to use it? This guide intro...
-
Healthy Living Tips -- Lose Weight and Feel Better | By koolmelee1 | in Diet & Nutrition
Have you let your weight get out of hand? I share some tips on how to lose weight one step at a time....
-
How to Create Bootable USB Drive (With Windows XP, Vista, Windows 7) | By koolmelee1 | in Programming
This article shows you how to create a bootable USB Flash drive that you can put any bootable Windows Operating Sys...
-
C# Tutorial - Lesson 2: Enumerations | By koolmelee1 | in Programming
C#, much like any other high level programming language, provides for enumerated types. This article explains what ...
-
C# Tutorials - Lesson 1 : Basic Data Types and Variables (int, double, float, etc...) | By koolmelee1 | in Programming
C# has many options to choose from as far as primitive data types go. This tutorial will give a summary of each, as...








No comments yet.