Primitive data types in java pdf




















This is used when int is not large enough to store the value. Note that you should end the value with an "L":. You should use a floating point type whenever you need a number with a decimal, such as 9. The float data type can store fractional numbers from 3. Note that you should end the value with an "f":. The double data type can store fractional numbers from 1. Note that you should end the value with a "d":.

The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations. A boolean data type is declared with the boolean keyword and can only take the values true or false :. Boolean values are mostly used for conditional testing, which you will learn more about in a later chapter.

The char data type is used to store a single character. However, some names cannot be used as they are used for something else in Java reserved words. Here's a list of them:. When naming your variable, using descriptive variable names are preferred instead of a random name like "d" or "sksksksksksk".

Also, notice how each variable has its data type. This is called a strongly typed language , more specifically statically typed. This is the opposite of a dynamically typed language , where variables can change partway through. Examples of legal and illegal variable names:. Here is an example of initializing variables of all of the data types. Note that there are sometimes scope modifiers with the variables, also called access modifiers. If there are no scope or access modifiers, this is called default access , which means the variable is accessible by all classes in that package also known as a folder in your computer.

Private access is where a variable, class, or method is only available to that own class. We already talked about public access when talking about the main method in 1. Finally, protected access with methods, variables, and classes is available to all classes in that package and any subclass as well we will talk about subclasses in unit 9.

We can also declare the variable without initializing it like this:. Some variables are constants , which means that they never change. We use the final modifier to show this and name the variable in all capitals.

Here is how we denote them in Java:. A primitive data type specifies the size and type of variable values, and it has no additional methods. The primitive data types are building blocks of data manipulation. There are eight types of Primitive Data Types, which are the following. Now we will read about them one by one. The boolean data type is defined with a boolean keyword.

This data type holds two values, either it is true or false. These values are primarily used in conditional cases like if the condition is satisfied, return true otherwise, return false. The char data type in Java is used to hold a single character in a variable. The size of each character is 2 bytes. The byte data type stores integer value between to If you know that your result of an equation will be in this range, it is better to use this data type because it will save your memory. The default size of this data type is 1byte.

The short data type is higher than the byte. It can store numbers between to The size of this data type is 2 bytes. As mentioned above, this data type should never be used for precise values, such as currency. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java. String class. String objects are immutable , which means that once created, their values cannot be changed.

The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such. You'll learn more about the String class in Simple Data Objects.

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null , depending on the data type.

Relying on such default values, however, is generally considered bad programming style. Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it.

Accessing an uninitialized local variable will result in a compile-time error. You may have noticed that the new keyword isn't used when initializing a variable of a primitive type. Primitive types are special data types built into the language; they are not objects created from a class. A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation.

As shown below, it's possible to assign a literal to a variable of a primitive type:.



0コメント

  • 1000 / 1000