/
Java Data Types Java Data Types

Java Data Types - PowerPoint Presentation

lois-ondreau
lois-ondreau . @lois-ondreau
Follow
444 views
Uploaded On 2016-03-19

Java Data Types - PPT Presentation

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 ID: 262254

variable data double types data variable types double type java conversion casting variables change integer stored int left assignment double

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Java Data Types" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Java Data Types

Data types, variable declaration, and initializationSlide2

Type

Size

UseInt4 bytesIntegerLong8 bytesBigger IntegerFloat4 bytesDecimal valuesDouble8 bytesPrecise decimals

TOO MANY DATA TYPES!!Slide3

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 typesSlide4

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 valueSlide5

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!Slide6

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’Slide7

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 digitCan’t be the same name as a Java keywordThey will be case sensitiveNo blank spaces allowed!Variable RulesSlide8

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!!Slide9

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

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!Slide11

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

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 CoolSlide13

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?Slide14

Java Data Types

Assignment and CastingSlide15

Assignment

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

int myInteger; // Declaration myInteger = 7; // AssignmentJava uses ‘=‘ character as the assignment operator. The value on the right side will be stored on the variable on the left.Slide16

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

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

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 longAnything 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.Slide19

What?

Consider this!

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

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

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

Your Mission

Questions 1-11 on the Unit 3 assignment sheet.