Data Types in Java

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

char is used for storing characters(letters).

boolean data type is used for variables that holds either true or false.

boolean: holds either true of false.

char: holds characters.

Literals in Java

A literal is a fixed value that we assign to a variable in a Program.

Primitive data types

In Java, we have eight primitive data types: Boolean, char, byte, short, int, long, float and double. Java developers included these data types to maintain the portability of java as the size of these primitive data types do not change from one operating system to another.

Integer Literal

Integer literals are assigned to the variables of data type byte, short, int and long.

Data type defines the values that a variable can take, for example if a variable has int data type, it can only take integer values. In java we have two categories of data type: Primitive data types and Non-primitive data types - Arrays and Strings are non-primitive data types.

Java is a statically typed language. A language is statically typed, if the data type of a variable is known at compile time. This means that you must specify the type of the variable (Declare the variable) before you can use it.

byte:

This can hold whole number between -128 and 127. Mostly used to save memory and when you are certain that the numbers would be in the limit specified by byte data type. Default size of this data type: 1 byte. Default value: 0

short:

This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767. Default size of this data type: 2 byte

Char and String Literal

Used for char and String type.

Float Literals

Used for data type float and double. Note: Always suffix float value with the "f" else compiler will consider it as double.

long

Used when int is not large enough to hold the value, it has wider range than int data type, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

byte, short, int and long data types are used for storing whole numbers.

float and double are used for fractional numbers.

double: Sufficient for holding 15 decimal digits

float: Sufficient for holding 6 to 7 decimal digits


Kaugnay na mga set ng pag-aaral

Standard Form of Linear Equations, Standard Form of Linear Equations, Linear equation forms, Slope and Slope Intercept Form, Point-slope form

View Set

L/A/H Insurance . C5.Life Insurance Policy Provisions . Questions

View Set

Chapter 22: AEROBIC RESPIRATION AND ENERGY PRODUCTION

View Set

Chap 5: Normal, Binomial, Poisson, and Exponential Distributions

View Set