This is not exactley a tutorial, but rather information you should know before you start using Java. I will be refering to in in future tutorials.
Unlike my favorite web-based programming language, PHP, Java requires you to declare your variable to a type.
int x = 0;
String y = “Hello World”;
Below are a list of all the data types in Java and a brief explanation of each.
char: Has the ability to hold a single letter. I’ve never once used this data type.
String: The data type string can hold any string including numbers, letters, and special characters. However you must note that the number 5 stored as a string is NOT evaluated as a number. Also for the longest time I always wondered why String was the only data type with a capital letter. I recently found out that String is a reference to a class. Its default value is evaluated to be “null.”
byte: This data type can hold any numeric integer from -128 to 127. Its default value is evaluated to be “0.”
short: This data type can hold any numeric integer from -32,768 to 32,767. Its default value is evaluated to be “0.”
int: The data type int can hold any numeric integer from -2,147,483,648 to maximum value of 2,147,483,647. Its default value is evaluated to be “0.”
long: The long data type can hold any numeric integer from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Its default value is evaluated to be “0L.”
float: The float data type is used for decimals. I believe it is 32 bytes and has the ability to hold 754 floating points. Its default value is “0.0f”
double: The double data type is also used for decimals. It has more precision than float and is often referred to as double-precision, hence the name. It is 64 bytes and also holds 754 floating points. Its default value is “0.0d”.
boolean: This data type can either store true or false. It is very useful for state functions and is often used in loops. Its default value is “false”.
For the most part, this information applys to other programing languages, but for our porposes, these are the specific data types in Java.
If not using large amounts of data, is it ok to not have to get an exact data type for your variable for basic needs?
I would usually pick an "int" for a basic data type for any number that I use...
for most programs it really dosnt matter, it would guess for large applications it would be more wise to use the "best" data types for the job. that way you dont allocate too much memory thats not being used. However with that said, ive only ever used boolean, String, int, and double![]()
Nice description of the datatypes.
One thing though is that these are just primitive data types and not objects. The nice thing about java is that it is object oriented, and so you create objects (which are like a more complex data type) and so you have virtually unlimited number of datatypes in reality....because you create them.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks