Java Data Types Data types, variable declaration, and initialization.

22
Java Data Types Data types, variable declaration, and initialization

Transcript of Java Data Types Data types, variable declaration, and initialization.

Page 1: Java Data Types Data types, variable declaration, and initialization.

Java Data TypesData types, variable declaration, and

initialization

Page 2: Java Data Types Data types, variable declaration, and initialization.

Type Size Use

Int 4 bytes Integer

Long 8 bytes Bigger Integer

Float 4 bytes Decimal values

Double 8 bytes Precise decimals

TOO MANY DATA TYPES!!

Page 3: Java Data Types Data types, variable declaration, and initialization.

Int and double data types suffice for most purposes.

There are more data types, but we don’t really care about that right now.

Fewer data types

Page 4: Java Data Types Data types, variable declaration, and initialization.

When you declare one of these variables, space is set aside in the computer’s memory to store a value for that variable.

Variables contain a value

Page 5: Java Data Types Data types, variable declaration, and initialization.

When writing numeric values, no commas can be used.

Decimal points, however can be used to denote a “floating point” value.

YOU SHALL NOT COMMA!

Page 6: Java Data Types Data types, variable declaration, and initialization.

Beware! There is also a system supplied class called “Double” in Java. This is different from “double”. To avoid potential errors, be sure to use a lower-case ‘d’. Same applies to other types like Integer.

‘Double’ and ‘double’

Page 7: Java Data Types Data types, variable declaration, and initialization.

They have to be typed. Ideally, should be initialized to some kind of

value. Should be given a descriptive name!

Name can contain letters, digits, $, and _ Can’t start with digit Can’t be the same name as a Java keyword They will be case sensitive No blank spaces allowed!

Variable Rules

Page 8: Java Data Types Data types, variable declaration, and initialization.

This is okay. It helps to begin all variable names

something such as “my”, both to differentiate from keywords (for Java), and for the reader to determine which variables were declared by the author.

You can’t name a double “double”, but you can name it “myDouble” because “myDouble” is not a keyword.

But I don’t know all the Java keywords!!

Page 9: Java Data Types Data types, variable declaration, and initialization.

It is syntactically okay for a variable to begin with uppercase letters, however this may get confusing. Generally, it is classes that should be named

with a capital letter. Therefore, variables should begin with a lower

case letter to avoid confusion with classes. “MyThing” for classes, “myThing” for variables.

Naming Conventions – Lowercase vs. Uppercase

Page 10: Java Data Types Data types, variable declaration, and initialization.

You may want to use compound words for the name of a variable. Though it requires more typing, it will be easier to understand the code where it’s being used later on. payRate, pay_rate, payrate… Note that the dash (-) is not used, because the

compiler will interpret as subtraction.

Naming Conventions – Descriptiveness!

Page 11: Java Data Types Data types, variable declaration, and initialization.

It is also good practice to initialize variables where they are declared. Ex: double tax_rate = .05;

If not initialized, a data type will be given a default value.

Even when a default value is provided by the system, it is helpful to initialize the variable yourself.

Initialization

Page 12: Java Data Types Data types, variable declaration, and initialization.

It is possible to set a variable to never change its value after initialization with the ‘final’ keyword. Ex: final int WEEKS_IN_A_YEAR = 52;

The idea is that the value isn’t expected to change.

Attempting to change the value will result in a compiler error.

It is also desirable to use all caps to distinguish from other variables.

Constants: Caps Lock is Cruise Control for Cool

Page 13: Java Data Types Data types, variable declaration, and initialization.

Why not simply declare a variable and never change it? Suppose you’ve forgotten how that variable

was supposed to be used, and you DO change it.

Better yet, suppose a user of your code tries to change it.

If someone were to accidentally change that value, there might be various (and undetectable) problems later on.

Constants: Why?

Page 14: Java Data Types Data types, variable declaration, and initialization.

Java Data TypesAssignment and Casting

Page 15: Java Data Types Data types, variable declaration, and initialization.

Assignment

Once a variable has been declared, it can be assigned a value. Like so…

int myInteger; // Declaration myInteger = 7; // Assignment Java uses ‘=‘ character as the assignment

operator. The value on the right side will be stored on the variable on the left.

Page 16: Java Data Types Data types, variable declaration, and initialization.

Assignments of Different Types

So, integers can be assigned to integers, doubles to other doubles…

BUT. A problem arises when the value on the right is not of the same type on the left.

Sometimes, the system converts the value into the correct type automatically.

Other times, it doesn’t.

Page 17: Java Data Types Data types, variable declaration, and initialization.

Automatic Conversion

Conversion goes as such: If the data type on the right cannot contain

more information than the data type on the left,

Automatic conversion occurs. Else, if the data type can contain more

information than the data type on the left, Conversion would lead to the loss of

info. This is not allowed by the system.

Page 18: Java Data Types Data types, variable declaration, and initialization.

Automatic Conversion

Conversion rules: Integers (4 bytes) can be stored in a long (8 bytes) Floats (4 bytes) can be stored in a double (8 bytes) Integer/long CAN be stored in float/double Long can be stored in a float. Float/double cannot be stored in an integer or long

Anything to the right of the decimal place would be lost. In general, you can store values from small containers

into bigger containers, but you can’t store values from large containers into smaller containers because those values might be too big.

Page 19: Java Data Types Data types, variable declaration, and initialization.

What?

Consider this! double taxRate = 0.05;

int weeksInaYear = 52; This would be allowed: taxRate = weeksInaYear; This is not allowed: weeksInaYear = taxRate;

Page 20: Java Data Types Data types, variable declaration, and initialization.

Casting

double myDouble = 0.05;int myInteger = 52;

In order to store the double into the integer type, casting would be required:

myInteger = (int) taxRate; Here, we explicitly cause a conversion from double

to integer types to occur. The value on the right is ‘cast’ to the type on the

left by putting the desired type in parenthesis, in front of the quantity on the right.

Page 21: Java Data Types Data types, variable declaration, and initialization.

With Casting Comes Responsibility

Casting functions almost like a contract. When the programmer casts between types,

he takes responsibility for what data is potentially lost due to casting.

In the case of casting from double to integer, all of the numerical data to the right of the decimal point would be lost. The data is truncated, and no rounding is performed.

5.2 would become 5 and 2.0 would become 2.

Page 22: Java Data Types Data types, variable declaration, and initialization.

Your Mission

Questions 1-11 on the Unit 3 assignment sheet.