Skip to main content

Data types in Java (Part 1)

>

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:
  1. Integers: This group includes byte, short, int, and long, which are for whole-valued signed numbers.
  2. Floating-point numbers: This group includes float and double, which represent numbers with fractional precision.
  3. Characters: This group includes char, which represents symbols in a character set, like letters and numbers.
  4. Boolean: This group includes boolean, which is a special type for representing true/false values.
Let us see each one in detail:
  1. 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.
      1. 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;
      2. Short:
        • Use: Similar to byte.
        • Declaration: short b, c=5;
      3. 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;
      4. 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

Relevant to YOU:

Popular posts from this blog

ASCII Code in Java (Part 1)

> The ASCII Code: (Part 1) ASCII stands for "American Standard Code for Information Interchange". As you may remember (Grade 9), computers only work with HIGH(1) and LOW(0) electrical states, known as bits, with correspond to mathematical Base 2 numbers. That is, computers only understand binary language. ASCII codes represent text (or other things) in computers. Assume that you are working with MS Word, or PPT or any other tool that uses text based inputs from user. You need to type a sentence that computer is not aware of. ASCII codes help to exchange this information between user and computer. When you type a character, it is converted into ASCII code first and then into Binary, which makes the computer understand what is to be typed. Hence every key on the keyboard has a specific ASCII code which is used to make the computer understand our language. If you press 4 from keyboard, then keyboard send the value 100 (value equival...

Characteristics of Java OR Java Buzzwords (Part 2)

> Java Properties: (Part 2) Robust: Java is robust because it is more reliable. It ensures the reliability by providing early checking for possible errors. It eliminates error causing constructs such as pointers. Java restricts the programmers in a few key areas, to force them to find mistakes early in program development. At the same time, Java frees from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks the code at compile time. However, it also checks your code at run time. Consider two main reasons of program failure: Memory management mistakes and Mishandled exceptional conditions (run time errors). Memory management is difficult and tedious in c/c++, the programmers have to manually allocate and free memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or, worse, try to ...