Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Expressions, Data Conversion, and Input
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
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?
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Computer programming Lecture#2 أ. إلهام باسندوه 1.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Chapter 2 Variables.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
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 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Topic 2 Elementary Programming
A variable is a name for a value stored in memory.
Chapter 2 Basic Computation
Chapter 4 Assignment Statement
Unit 2 Elementary Programming
Elementary Programming
Multiple variables can be created in one declaration
Chapter 3 Assignment Statement
Chapter 3 Java Input/Output.
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
Starting JavaProgramming
Unit-2 Objects and Classes
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Chapter 3 Input/Output.
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Review of Java Fundamentals
Presentation transcript:

Java – Variables and Constants By: Dan Lunney

Declaring Variables All variables must be declared before they can be used A declaration takes the form: Example: int length; It is good practice to declare related variables together Example: int length, width;

Variables cont. Variable names must start with a letter (should be lowercase) Variable names may include numbers and some special characters – but this practice is not encouraged Variables are declared at the beginning of classes and methods

Primitive Data Types int – positive or negative integers between -2.1 billion to + 2.1billion double – positive and negative numbers that may contain a decimal (sometimes refered to as floating point) char – a single character boolean – true or false

Abstract Data Types Abstract data types typically come from a class – either a built in class that is imported or a class written by the programmer A variable declared using a class is called an object = new (args) Example: Scanner input = new Scanner(System.in);

Java Packages Some classes in Java that are already written come in Java Packages The only package that loads automatically in java is the java.lang package used to define the java language Other packages must be imported to be used by the java program Example: import java.util.Scanner; This imports the scanner class used to get input from the keyboard

Scanner Class Methods The scanner class comes with several methods to read info from the input stream: next() returns a string from the input stream (to the first blank space) nextLine() returns the next line from the screen nextInt() returns the next integer nextDouble() returns the next double nextBoolean() returns the next boolean close() closes the input stream

Type Casting Type casting converts a number of one type to a number of a different type i.e. a double to and int or vice versa Example: x = I + int(d); The d which was declared as a double type is turned into an integer type. Therefore any decimal portion is truncated Example: area = double(length) * double(width) This turns length and width to doubles for this calculation

NumberFormat Class Imported with the line: import java.text.NumberFormat; Example: NumberFormat money = new NumberFormat.getCurrencyInstance(); This line sets up an object used to format numbers into money Example : System.out.println(money.format(dollars)); This will take a variable named dollars and print it to the screen as a currency with the format $X.XX

Assignment Operators In an assignment statement the variable name on the left side of the = sign is given the value of the expression on the right. Examples: intPlayer = 12; intPlayer = intPlayer + 2;

Constants A constant is a name used to store a value that does not change Should be written in all uppercase letters It is declared as follows: final Example: final double PI; A constant can only be assigned a value once. Trying to change the value of a constant will result in an error Constants are declared at the beginning of methods and classes before any variables

Identifiers and Keywords Identifiers and keywords are words reserved by the java language These words cannot be used as variable or constant names There is a list of 51 words that java uses as identifiers and keywords. The list can be found on page 88. Examples: for, if, while, goto, int, double, etc