>
Data Types: The type of data a variable can hold.
The 8 primitive datatypes are grouped together into 4 main groups:
Data types in Java: (Part 1)
Data Types: The type of data a variable can hold.
The 8 primitive datatypes are grouped together into 4 main groups:
- Integers: This group includes byte, short, int, and long, which are for whole-valued signed numbers.
- Floating-point numbers: This group includes float and double, which represent numbers with fractional precision.
- Characters: This group includes char, which represents symbols in a character set, like letters and numbers.
- Boolean: This group includes boolean, which is a special type for representing true/false values.
- Integers:
- Java defines four integer types: byte, short, int, and long. All of these are signed, positive and negative values. Java does not support unsigned, positive-only integers.
- The width of an integer defines the behavior it defines for variables and expressions of that type. The Java run-time environment is free to use whatever size it wants, as long as the types behave as you declared them.
- Byte:
- Use: useful when working with a stream of data from a network or file. They are also useful when working with raw binary data that may not be directly compatible with Java’s other built-in types.
- Declaration: byte b=1, c=5;
- Short:
- Use: Similar to byte.
- Declaration: short b, c=5;
- int:
- Use: useful in control loops as a control variable, also used to index arrays. If an integer expression is using bytes, shorts, ints, and literal numbers, the entire expression is promoted to int before the calculation is done. For number counting, integer math, and solving any arithmetic expression, mostly used data type is integer.
- Declaration: int i , j=5;
- Long:
- Use: used when integer type is not enough to hold the desired value. The range of a long is quite large. This makes it useful when big, whole numbers are needed. They can be represented by appending a letter ‘L’ at end of declaration.
- Declaration: long a; or long a = 500L;
Comments
Post a Comment