java data types data types, variable declaration, and initialization
TRANSCRIPT
![Page 1: Java Data Types Data types, variable declaration, and initialization](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/1.jpg)
Java Data TypesData types, variable declaration, and
initialization
![Page 2: Java Data Types Data types, variable declaration, and initialization](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/2.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/3.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/7.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/8.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/9.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/14.jpg)
Java Data TypesAssignment and Casting
![Page 15: Java Data Types Data types, variable declaration, and initialization](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/15.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/19.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/20.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/21.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032709/56649ea95503460f94bad6a9/html5/thumbnails/22.jpg)
Your Mission
Questions 1-11 on the Unit 3 assignment sheet.