Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.

Slides:



Advertisements
Similar presentations
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Advertisements

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.
03 Data types1June Data types CE : Fundamental Programming Techniques.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Some basic I/O.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Review Java.
Expressions, Data Conversion, and Input
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Java Fundamentals Expanded SE1021 Dr. Mark L. Hornick 1.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2 Elementary Programming
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
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.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
© A+ Computer Science - Variables Data Types © A+ Computer Science - Lab 0B.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
USER INTERACTION AND VARIABLES Tarik Booker CS 290 California State University, Los Angeles.
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.
Chapter 2 Basic Computation
Elementary Programming
Introduction to Computer Science / Procedural – 67130
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Data Types The type states how much and what kind of data the variable can store. integers- whole numbers no fractional parts int, short, long floating.
Java Programming: From Problem Analysis to Program Design, 4e
INPUT STATEMENTS GC 201.
IDENTIFIERS CSC 111.
Starting JavaProgramming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
© A+ Computer Science - VARIABLES © A+ Computer Science -
Chapter 2: Basic Elements of Java
Fundamentals 2.
Chapter 2: Java Fundamentals
Variables Data Types Lab 0B
CS2011 Introduction to Programming I Elementary Programming
Chapter 2: Java Fundamentals cont’d
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Variables and Types Java Part 3

Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of the object.  The type associated with these variables can be built in or user-defined.

8 Intrinsic (Primitive) Types  int  integer (4 bytes)  double  number with a decimal (8 bytes)  boolean  true / false (2 bytes)  char  single character (2 bytes)  byte (1 byte)  short (2 bytes)  long (8 bytes)  float (4 bytes) Note: these all begin with a lowercase letter

Abstract Types  String  This is a class defined in the java library and is used to hold strings of characters.  For ex, name=“Joe Smith”  Font  used to change the font style of your output Note: These begin with an uppercase letters – denotes a class name.

User-Defined Types  Another type of abstract data  You can declare member variables to be of user-defined types, such as any class that you have created.  For ex, if you create a Widget class, and save it, you can declare things of type Widget in another project, or this same project.

Declaring Variables int length; int length, width; int length = 10; In the last statement, the variable is declared and initialized in one statement. Monster m = new Monster(); This is how you declare a variable of a user- defined type.

Referencing Variables Intrinsic variables store the value of the variable. Object variables (such as type String, or anything you create) store the reference (address) to the data, not the data itself. This will become important when you are changing the values of an object later on.

What is a reference? Monster m = new Monster(); m Monster Object 0xF5 m stores the address of a Monster

What is a primitive variable? int numDays = 365; numDays 365 numDays stores an integer value

Real Numbers

Reals – predict the output double one = 99.57; double two = 3217; float three = 23.32f; System.out.println(one); System.out.println(two); System.out.println(three); OUTPUT

double one = 120.7; System.out.println(one); one = 125; System.out.println(one); System.out.println((int)one); OUTPUT

Characters  Character variables are used to store a single letter.  Character variables are actually integers. char let = 'A'; char fun = 65; char test = 'a'; char go = 97; char what = 48;

Ascii Values ASCII values you MUST know!!! ‘A’  65 ‘a’  97 ‘0’  48 char letter = 97; out.println(letter); //outs a letter = 'A'; out.println(letter); //outs A

Characters char alpha = 'A'; char ascii = 65; char sum = 'B' + 1; System.out.println(alpha); System.out.println(ascii); System.out.println(sum); System.out.println('B'+1); OUTPUT A C 67

Booleans  Booleans can only store true or false boolean go = true; System.out.println(go); boolean stop = false; System.out.println(stop); OUTPUT true false

Strings  String is a class that stores a group of characters String dude = "hello world"; String buddy = "whoot - \\\\\\\\\\\\"; System.out.println(dude); System.out.println("buddy = " + buddy); OUTPUT hello world buddy = whoot - \\\\\\

Variables Assignment The statement receiver = 57; assigns the value 57 to the variable receiver. It is assumed that the variable receiver has already been declared before this point. int num = 82; declares the variable and assigns it a value in one statement.

Assignment Practice int number = 75, bigNum=99; double hTownTax = 8.25; char bigA = 'A', littleA = 'a'; boolean isPrime = false; String s = "abc"; System.out.println(number); System.out.println(bigNum); System.out.printf("%.2f\n",hTownTax); System.out.println(bigA); System.out.println(littleA); System.out.println(isPrime); System.out.println(s); OUTPUT A a false abc

How Big and Small can my variables go? The MIN_VALUE and MAX_VALUE fields store the minimum and maximum values that can be stored in a particular type. System.out.println(Byte.MIN_VALUE); System.out.println(Byte.MAX_VALUE); System.out.println(Short.MIN_VALUE); System.out.println(Short.MAX_VALUE); OUTPUT

Mixing Types  Java is a stong typed language.  You must pay attention to a variable’s type when assigning a value.  Integers cannot store decimals unless a cast is used. int one=90; char letter= ‘A’; char let= 97; one=letter; letter=let; one=let;

 When assigning values from one variable to another, data types and data type sizes play an important part.  Large size types can be assigned same size type and smaller type values.  Smaller size types cannot be assigned larger type values.  16 bit = 16 bit and smaller //legal  8 bit = 16 bit and smaller //illegal without a cast()

int one = 90; double dec = 234; char letter = 'A'; System.out.println( one ); one = letter; //char to int System.out.println( one ); one = 'A'; //char to int System.out.println( one ); System.out.println( dec ); dec = one; //int to double System.out.println( dec ); OUTPUT Data type sizes often determine if assignment is legal. 32 bit == 32 bit

AutoBoxing and AutoUnboxing In JAVA, you have 8 primitive data types. All other variables in Java are reference variables. References refer to objects. Monster m = new Monster();

For each of the primitive types, there is a corresponding wrapper class. primitiveobject byteByte shortShort intInteger longLong floatFloat doubleDouble charCharacter booleanBoolean

Box/Unbox Integer numOne = 99; Integer numTwo = new Integer(99); =99; =new Integer(99); These two lines are equivalent.

Box/Unbox Double numOne = 99.1; Double numTwo = new Double(99.1); =99.1; =new Double(99.1); These two lines are equivalent. With the introduction of Java 5, the new Double() Object instantiation code happens in the background. Java takes care of these details, but does not show the work.

GUI Help //gui example import javax.swing.JOptionPane; public class GuiHelp { public static void main(String args[]) {{ int one, two, total; one = Integer.parseInt(JOptionPane.showInputDialog("Enter an integer :: ")); two = Integer.parseInt(JOptionPane.showInputDialog("Enter an integer :: ")); total = one + two; JOptionPane.showMessageDialog(null,"Total ::" + total); System.out.println(total); }

Constants  In java, we refer to constants as finals.  A declaration would look like: final double PI = 3.14;  The name of the final would be in UPPERCASE letters.  Use finals whenever you have values that don’t change while the program is running.

Assignment Operators  total = total + number;  total += number;  numPlayers = numPlayers -3;  numPlayers -= 3; Assignment operators include: =, +=, -=, *=, /=, %=

Increment and Decrement  The increment operator (++) means add by 1  The decrement operator (--) means decrease by 1  These can be prefix or postfix. Prefix: ++x;  increment, then fetch Postfix: x++;  fetch, then increment There is a difference…

Concatention  The + sign is used to concatenate strings and/or variable values. Relational Operators !=  not equal to ==  equal to

format()  This can be used to format numeric values.  A specifier takes the form: %[alignment][width][.decimal]f where [.decimal] indicates the number of decimal places and f indicates a floating point number. For an integer: %[alignment][width]d %  start of specifier [alignment]  skip for right alignment, - for left align [width]  num of characters used s  indicates String

NumberFormat Class  import java.text.NumberFormat; money=NumberFormat.getCurrencyInstance(); number=NumberFormat.getIntegerInstance(); decimal = NumberFormat.getNumberInstance(); percent=NumberFormat.getPercentInstance();

Getting Input from User  An input stream is a sequence of characters received from an input device, such as a keyboard.  import java.util.Scanner to get input from the keyboard.  Use System.in to read in the input

Scanner Methods  next()  returns a string (no spaces)   use after reading in numeric data as nextLine() will read in the EOL character, not the next actual line  nextLine()  returns string up to EOL  nextInt()  returns the int from the stream  nextDouble()  nextBoolean()  close()  closes the input stream

Assignments  Glencoe book, p 130, #5 – use wordpad  Worksheets  Variables Wk 0-2  Assignments from weebly