Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
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 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2: Introduction to C++.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
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.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
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.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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.
Primitive Variables.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
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.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
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.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Chapter 2: Introduction to C++
Java Variables and Types
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Chapter 2: Introduction to C++.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Just Enough Java 17-May-19.
Presentation transcript:

Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings

2 Comments Comments are used to make code more understandable to humans Java ignores comments // this is a single line comment /* this is * a multi-line * comment */

3 Literals Literals are the values we write in a conventional form whose value is obvious Welcome to DrJava (Interaction tab in Dr. Java) > 3 // An integer has no decimal point > 10.5 // a floating point double >‘a’ // a character has single quotes > true // The boolean literals are of two types: true, false > “hello world” // A string literal

4 Arithmetic Operators + to indicate addition - to indicate subtraction * to indicate multiplication / to indicate division % to indicate remainder of a division (integers only) parentheses ( ) to indicate the order in which to do things

5 Relational Operators == equal to != not equal to < less than > greater than <= less than equal to >= greater than equal to Note: Arithmetic comparisons result in a Boolean value of true or false

6 Boolean or Logical Operators In English, complicated conditions can be formed using "and", "or", and "not" E.g. "If there is a test and you did not study for it...". "And", "or", and "not" are Boolean operators, and they exist in Java as well || -> OR operator  true if either operand is true && -> AND operator  true only if both operands are true ! -> NOT operator  Is a unary operator – applied to only one operand  reverses the truth value of its operand

7 Expressions, Operators, Values Welcome to DrJava > 3 3 > >‘a’ == ‘A’ // == Equality operator false > true && false // using the logical AND > true || false // true (using the logical OR)  An expression has a value  An expression may consist of literals and operators  Given an expression, DrJava prints its value Later we’ll see that an expression may contain other things Such as variables, method calls …

8 Values, Types and Expression Value: Piece of data 23, 10.5, true, ‘a’ Type: Kind of data: integer, floating point, boolean (true/false), character An expression has a value or rather evaluates to a value 23 -> * 4 -> > 12.5 (3 * 4 ) / 15 -> 0 true && false -> false -> why zero?

9 Types: a very important concept! All data values in Java have a type The type of a value determines: How the value is stored in memory What operations make sense for the value How the value can be cast (converted) to related values Note: Types are very helpful in catching programming errors

10 Primitive types Values that Java knows how to operate on directly We will work with 4 of Java’s 8 primitive types Integer ( int ) -142 Fractional (floating point) number ( double ) E8 Character ( char ) 'J' ' 山 ' Truth value ( boolean ) truefalse Java’s other types are: byte, short, long, float

11 Storage Space for Numerics Numeric types in Java are characterized by their size: how much memory they occupy Integral types Floating point types sizerange byte1 byte-128: 127 short2 bytes-32768:32767 char2 bytes0:65535 int4 bytes : long8 bytes… smallest > 0 4.9E E E3088 bytesdouble 3.4E384 bytefloat largestsize

12 Another Important Type: String A String is an Object, not a primitive type Java also has objects - cover objects later String is composed of zero or more char s A String is a sequence of characters enclosed by double quotes "Java" "3 Stooges" " 富士山 “ + means concatenation for strings "3" + " " + "Stooges"  “ 3 Stooges ” Automatic conversion of numbers to strings 3 + " " + "Stooges"  “ 3 Stooges ”

13 Variables A variable is a named place in memory used to store a value Variable must always be associated with type It tells the computer how much space to reserve for the variable Example: int age;

14 Declaring variables All variables must be declared before being used With a declaration statement Declaration statement Specifies the type of the variable; Consists of type and variable name followed by semicolon(;) Examples: int seats; double averageHeight; boolean isFriday; String houseName;

15 Storing value into Variables To store values into variable we use the assignment operator i.e. “ = “ Variable = Expression; -> assignment statement Important Assignment statement must end with a semicolon(;) When a variable is assigned a value, the old value is discarded and totally forgotten Examples: seats = 150; averageHeight = ( )/3; isFriday = true; houseName = ”gryffindor";

16 Variable value and type The value of a variable may be changed : x = 57; However its type may not x = true; // this causes an error, compiler will complain

17 Identifiers Identifiers are names that you as the coder make up variable names also class and method names – next topic to cover Variable names may consist of alphanumeric characters and the underscore _. A variable name should be a noun that starts with an lowercase letter: sum size If the name has multiple words, capitalize the start of every word except the first firstName lastName Constants (variables that don’t change) should have all- caps. final int NORTH = 0; final int MILES_PER_GALLON = 32;

18 Naming conventions kindpart-of-speechidentifier variablenoun initialSpeed constantnoun MAXIMUM_SPEED Naming Convention is a style rule Java compiler will not complain if the rule is followed This for consistency and readability of programs especially by others If you do not follow the rule you get penalized in grading!

19 Initializing Variables It’s good idea to declare and initialize a variable in one statement double MILES_PER_HOUR = 60.5; String myName = ”Diana Palsetia";

20 Integer Division > 10.0 / > 10.0 / > 10 / > 10 / 3 3 > (double)10 / 3 // 10 is “cast” to a double > 10 / (double) 3 // 3 is “cast” to a double > (double)(10/3) // (10/3) is “cast” to a double 3.0 Integer division truncates!

21 Examples of String creation > String s2 = "hello"; > s2 + " you!" "hello you!" > s2 = "The result is " + 100; > s2 "The result is 100"

22 System.out.println( String ) Method that prints characters to terminal screen Different from having a method return a String Useful in finding semantic errors in a program System.out.println(“hello world”); System.out.print(“x is “ + x);

23 Recap An Expression Has a value Consists literals and operators – FOR NOW! A Statement (declaration and assignment) Must end with semicolon(;) Tells or commands the computer to do something Comments are ignored by the computer They are explanations of your program for human beings to read

24 Spaces You should put a single space around every binary operator, including comparisons and = Example: perimeter=2*(width+height); Do not put spaces just inside parentheses: perimeter = 2 * (width + height); //bad These are style rules, not Java rules If you break these rules, your program will still compile This is done so that your code is readable and clear If you do not follow the rule you get penalized in grading!

25 Lab Work Complete the lab exercise “Expressions and Statements”