Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java Data Types Data types, variable declaration, and initialization."— Presentation transcript:

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

2 TypeSizeUse Int4 bytesInteger Long8 bytesBigger Integer Float4 bytesDecimal values Double8 bytesPrecise decimals TOO MANY DATA TYPES!!

3  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

4  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

5  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!

6  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’

7  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

8  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!!

9  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

10  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!

11  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

12  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

13  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?

14 Java Data Types Assignment and Casting

15 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.

16 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.

17 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.

18 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.

19 What?  Consider this!  double taxRate = 0.05; int weeksInaYear = 52;  This would be allowed:  taxRate = weeksInaYear;  This is not allowed:  weeksInaYear = taxRate;

20 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.

21 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.

22 Your Mission  Questions 1-11 on the Unit 3 assignment sheet.


Download ppt "Java Data Types Data types, variable declaration, and initialization."

Similar presentations


Ads by Google