Use a variable to store a value that you want to use at a later time

Slides:



Advertisements
Similar presentations
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
IT151: Introduction to Programming
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Chapter 2 Using Objects. These slides for CSE 110 Sections are based in part on the textbook-authors ’ slides, which are copyright 2003 by Cay Horstmann.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
How Create a C++ Program. #include using namespace std; void main() { cout
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Chapter 2 types, variables, methods Pages Horstmann.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher Fundamental Data Types 09/09/13.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
Classes CS 21a: Introduction to Computing I First Semester,
General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Primitive Data Types. Identifiers What word does it sound like?
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Copyright Curt Hill Variables What are they? Why do we need them?
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Variables. Some Rules of Variable Naming Convention : Variable names are case-sensitive. A variable’s name can be any legal identifier. It can contain.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
A Sample Program #include using namespace std; int main(void) { cout
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.
COM S 207 Type and Variable Instructor: Ying Cai Department of Computer Science Iowa State University
A final variable is a constant
John Woodward A Simple Program – Hello world
Working with Java.
Crash course in the Java Programming Language
Java Variables and Types
Chapter 3 GC 101 Java Fundamentals.
Console Output, Variables, Literals, and Introduction to Type
Chapter 4 – Fundamental Data Types
University of Central Florida COP 3330 Object Oriented Programming
Chapter 1 – Introduction
Variables, Printing and if-statements
Chapter 4 Procedural Methods.
Chapter Three - Implementing Classes
IDENTIFIERS CSC 111.
Chapter 2 Using Objects.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
CMSC 202 Java Primer 2.
Chapter 2: Java Fundamentals
Chapter 7 Procedural Methods.
Expressions and Assignment Statements
elementary programming
Chapter 2: Java Fundamentals
Classes CS 21a: Introduction to Computing I
Chapter 5 – Decisions Big Java by Cay Horstmann
Each object belongs to a class
1.7 Errors Compile-time error: A violation of the programming language rules that is detected by the compiler Example: System.ou.println("Hello, World!);
6.2 for Loops Example: for ( int i = 1; i
Parameter: an input to a method
JAVA. Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of platforms, such.
Presentation transcript:

Use a variable to store a value that you want to use at a later time 2.2 Variables Use a variable to store a value that you want to use at a later time A variable has a type, a name, and a value: String greeting = "Hello, World!” int width = 13; double area = 16.877; Variables can be used in place of the values that they store: System.out.println(greeting); // Same as System.out.println("Hello, World!”) System.out.println(width); // Same as System.out.println(13) Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Variables It is an error to store a value whose type does not match the type of the variable: String greeting = 20; // ERROR: Types don’t match Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Variable Declarations Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Identifier: name of a variable, method, or class Identifiers Identifier: name of a variable, method, or class Rules for identifiers in Java: Can be made up of letters, digits, and the underscore (_) and dollar sign ($) characters (don’t use it) Cannot start with a digit Cannot use other symbols such as ? or % Spaces are not permitted inside identifiers You cannot use reserved words such as public They are case sensitive Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

By convention, variable names start with a lowercase letter Identifiers By convention, variable names start with a lowercase letter “Camel case”: Capitalize the first letter of a word in a compound word such as farewellMessage By convention, class names start with an uppercase letter Do not use the $ symbol in names — it is intended for names that are automatically generated by tools Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Syntax 2.1 Variable Declaration Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Variable Names Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Self Check 2.5 Which of the following are legal identifiers? Greeting1 g void 101dalmatians Hello, World <greeting> Answer: Only the first two are legal identifiers.

Self Check 2.6 Define a variable to hold your name. Use camel case in the variable name. Answer: String myName = "John Q. Public";