Variables and Datatypes
Boolean values are mostly used for?
conditional testing
Declare variable then assign and print the value later:
int myNum; myNum = 15; System.out.println(myNum);
Create a variable to store a number: Declare and print variable 'myNum', type 'int' and assign value 15:
int myNum = 15; System.out.println(myNum);
Assigning a new value to an existing variable overwrites the previous value: Change the value of myNum from 15 to 20:
int myNum = 15; myNum = 20; // myNum is now 20 System.out.println(myNum);
Using float or double what is the precision for each, and which is safer?
- precision indicates how many digits after the decimal point. (IT IS SAFER TO USE DOUBLE FOR MOST CALCULATIONS) - precision of float is only six or seven decimal digits, - double variables have a precision of about 15 digits.
Booleans (true or false)
A boolean data type is declared with the boolean keyword and can only take the values true or false: boolean isJavaFun = true; boolean isFishTasty = false; System.out.println(isJavaFun); // Outputs true System.out.println(isFishTasty); // Outputs false
Scientific Numbers (e = power of 10)
A floating point number can also be a scientific number with an "e" to indicate the power of 10: float f1 = 35e3f; double d1 = 12E4d; System.out.println(f1); System.out.println(d1);
Numbers Primitive number types are divided into two groups Integer and Floating point types explain them: (The type you should use, depends on the numeric value.)
Integer types stores whole numbers, positive or negative, without decimals. - byte, short, int and long. Floating point types store numbers with fractional parts (decimals). - float and double.
Byte (integer type)
The byte data type can store whole numbers from -128 to 127. Saves memory over other interger type when the value needed is within -128 and 127: byte myNum = 100; System.out.println(myNum);
Final Variables Add the final keyword to prevent overwriting values (declaring it a "constant" readonly):
final int myNum = 15; myNum = 20; // will generate an error: cannot assign a value to a final variable
The numeric types most used for numbers are?
int (whole) and double (floating point).
Declare variables for the 5 types:
int myNum = 5; float myFloatNum = 5.99f; char myLetter = 'D'; boolean myBool = true; String myText = "Hello";
Java Data Types A variable in Java must be a specified data type name and give and example of the 5 most used:
int myNum = 5; // Integer (whole number) float myFloatNum = 5.99f; // Floating point number char myLetter = 'D'; // Character boolean myBool = true; // Boolean String myText = "Hello"; // String
Declare Many Variables Declare more than one variable of the same type, use a comma-separated list:
int x = 5, y = 6, z = 50; System.out.println(x + y + z);
For numeric values, use the + character as a mathematical operator and print the result: use x and y and the variable name.
int x = 5; int y = 6; System.out.println(x + y); // Print the value of x + y
Non-primitive data types are called?
reference types because they refer to objects.
Primitive Data Types?
A primitive data type specifies the size and type of variable values, and it has no additional methods.
Strings (String data type)
- Stores a sequence of characters surrounded by " ": - A non-primitive data type, b/c it refers to an object. - The String object has methods used to perform operations on strings. String greeting = "Hello World"; System.out.println(greeting);
Double (floating point type)
Store fractional numbers from 1.7e−308 to 1.7e+308. The value ends with a "d": double myNum = 19.99d; System.out.println(myNum);
Short (integer type)
Can store whole numbers from -32768 to 32767: short myNum = 5000; System.out.println(myNum);
What are the eight primitive data types in Java?
Data Type Size Description byte 1 byte Stores whole numbers from -128 to 127 short 2 bytes Stores whole numbers from -32,768 to 32,767 int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647 long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits boolean 1 bit Stores true or false values char 2 bytes Stores a single character/letter or ASCII values
The general rules for constructing names for variables (unique identifiers) are:
Names - can contain letters, digits, underscores, and dollar signs - must begin with a letter - should start with a lowercase and cannot contain whitespace - can begin with letters $ or _ (not digits) - case sensitive ("myVar" and "myvar" are different) - Reserved words (like int or boolean) can't be used as names
Data types are divided into two groups what are they?
Primitive data types - byte, short, int, long, float, double, boolean and char Non-primitive data types - String, Arrays and Classes
The main difference between primitive and non-primitive data types are:
Primitive types - Predefined in Java - Always a value - Can't not call methods to perform operations - Starts with a lowercase letter - Size depends on the data type Non-Primative type - Created by the programmer not defined by Java (except for String). - Can be null - Can be used to call methods to perform certain operations - Starts with an uppercase letter. - All have the same size.
Float (floating point type)
Stores fractional numbers from 3.4e−038 to 3.4e+038. The value ends with a "f": float myNum = 5.75f; System.out.println(myNum);
In Java, there are different types of variables, for example: - String - int - float - char - boolean
String - stores text, such as "Hello". String values are surrounded by double quotes int - stores integers (whole numbers), without decimals, such as 123 or -123 float - stores floating point numbers, with decimals, such as 19.99 or -19.99 char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes boolean - stores values with two states: true or false
Use the + character to add and print two variables: String firstName = "John "; String lastName = "Doe";
String fullName = firstName + lastName; System.out.println(fullName);
Displaying Variables The println() method is often used to display variables. To combine both text and a variable, use the + character:
String name = "John"; System.out.println("Hello " + name);
Create a variable to store text: Declare and print variable 'name', type 'String' and assign value "John":
String name = "John"; System.out.println(name);
What are examples of non-primitive types?
Strings, Arrays, Classes, Interface, etc.
Floating Point Types
The floating point type is used when for numbers with a decimal, ex: 3.14515.
int (integer type)
The int data type can store whole numbers from -2147483648 to 2147483647. In general, the int data type is the preferred data type when creating variables of numeric value. int myNum = 100000; System.out.println(myNum);
Long (integer type)
The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. Its used when int is not large enough to store the value. The value ends with an "L": long myNum = 15000000000L; System.out.println(myNum);
Java Identifiers All Java variables must be identified with unique names. Explain these unique names called identifiers and how they're used
They can be short names (like x and y) or more descriptive names (age, sum, totalVolume). Use descriptive names in to create understandable and maintainable code: // Good int minutesPerHour = 60; // OK, but not so easy to understand what m actually is int m = 60;
char data type (Characters)
Used to store a single character using single quotes, like 'A' or 'c': char myGrade = 'B'; System.out.println(myGrade); You can use ASCII values to display certain characters: char a = 65, b = 66, c = 67; System.out.println(a); System.out.println(b); System.out.println(c); Tip: A list of all ASCII values can be found in our ASCII Table Reference.
Explain Java Variables
Variables are containers for storing data values.
Declaring (Creating) Variables Specify the type and assign it a value: - Syntax: type variable = value;
Where type is one of Java's types (such as int or String), and variable is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.