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

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Expressions, Data Conversion, and Input
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
CPS120: Introduction to Computer Science
IB Computer Science Unit 1 – Introduction to Java Variables.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Mathematical Calculations in Java Mrs. G. Chapman.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Java Data Types Assignment and Simple Arithmetic.
Primitive Data Types. Identifiers What word does it sound like?
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 2 Variables.
Mathematical Calculations in Java Mrs. C. Furman.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CPS120: Introduction to Computer Science Variables and Constants.
Introduction ABAP Fields and Variables. Slide 2 Fields (Introduction) In ABAP, fields (or data objects) are named locations in memory Variables store.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
1 2. Program Construction in Java. 01 Java basics.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Primitive Types Four integer types: Two floating-point types:
A variable is a name for a value stored in memory.
Chapter 2 Basic Computation
Chapter 2 Elementary Programming
Multiple variables can be created in one declaration
Chapter 2 Basic Computation
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Numerical Data Types.
Unit-1 Introduction to Java
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Expressions and Assignment
Introduction to Primitives
Introduction to Primitives
Java Programming Language
Primitive Types and Expressions
Unit 3: Variables in Java
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Java Data Types Data types, variable declaration, and initialization

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

 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

 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

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

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

 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

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

 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

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

 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

 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

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

Java Data Types Assignment and Casting

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.

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.

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.

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.

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

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.

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.

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