Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

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.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CMT Programming Software Applications
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
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.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Primitive Variables.
Mathematical Calculations in Java Mrs. G. Chapman.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
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.
Variables in Java x = 3;. What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of.
Chapter 2 Variables.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
1.2 Primitive Data Types and Variables
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.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
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.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
1 2. Program Construction in Java. 01 Java basics.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 2 Variables.
Chapter 2 Basic Computation
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Computer Science 3 Hobart College
Type Conversion, Constants, and the String Object
Chapter 2 Basic Computation
Chapter 2.
IDENTIFIERS CSC 111.
Examples of Primitive Values
Introduction to C++ Programming
Learning Outcomes –Lesson 4
Chapter 2 Variables.
Building Java Programs
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Like Terms.
Variables and Constants
Primitive Data Types and Operators
Presentation transcript:

Java Data Types

Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or false eg. true – int, byte, short, long: used to store whole numbers eg. 4 – float, double: used to store fractional numbers eg. 7.1

Primitive Data Types Pair the following data types to their examples charint boolean float double char boolean int 827 % U 9 false true

Primitive Data Types To create and use a primitive data type we need to declare the data type to be used We then give it a name Lastly we give it a value int age = 17; data type identifier value Note: when declaring objects, we use a capital letter for the object type When declaring variables or primitive data types, we use a lower case letter for the data type

Mathematics The data types that deal with numbers can be used to calculate the solutions to mathematical equations Eg. int result = 6 + 2; The variable ‘result’ will then contain the value 8

Mathematics We can use the following operands: + Add -Subtract *Multiply / Divide %Modulus – remainder after dividing

Mathematics Remember that when you do a calculation you must have a variable ready to store the result. Complete the following calculations and indicate what would be stored in the variable int resultA = 7 * 2; int resultB = 23%5; int resultC = 16/4; int resultD = resultA + resultC * resultB;

Mathematics When I use an int and a float, my answer will be a float. If I try to store the answer in the wrong data type I will get an error about “Cannot cast...”

Type Casting We can ‘cast’ data types to change into other data types. If I were to cast a float (4.5) to an int, it would truncate all decimals and keep the whole number (4) If I were to cast an int (4) to a float, it would add decimals, and since it has no extra information that will be.0 (4.0)

Type Casting To cast a data type, you put the type in brackets before the value you want to cast. The two examples from the previous slide: 4.5  4 int solutionA = (int)4.5; 4  4.0 float solutionB = (float)4;

Type Casting In order of operations, e.g. BODMAS, casting is the same as brackets… Eg. int result = (int)5.5 * 3; this will truncate 5.5 and leave 5 it will calculate 5 * 3 it will store the result in the variable ‘result’ If we wanted to calculate 5.5 * 3 and get an integer result we would have: int result = (int) (5.5 * 3); this calculates 5.5 * 3 (16.5), then truncates the result (16)

Displaying output To get your results to appear on the screen, you must use a drawString However, if you put the variable name in the quotes, it will simply print out the variable name To avoid this you need to concatenate the variable to a string using +

Displaying output For example: g.drawString(“The result is: “+result, 20, 20); In the brackets of the drawString anything before the first comma is text to be displayed But we must make one big string to display by adding (+) all the pieces of information together