Basic Java Syntax Comments Basic data types Operators and assignment.

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

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
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.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
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.
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.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
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.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
13 X 11 Java Lecture 2 CS 1311 Beginning Java (Sort of Boring but Oh So Necessary Syntax Stuff) 13 X 11.
Variables & Operators 1 Variables Store information needed by the program Must have a TYPE int - can only store a number without a fractional part float,
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
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.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
CPS120: Introduction to Computer Science Variables and Constants.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Java-02 Basic Concepts Review concepts and examine how java handles them.
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.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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
Chapter 7: Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Modified from Sharon Guffy
Type Conversion, Constants, and the String Object
Chapter 2 Basic Computation
Variables ,Data Types and Constants
Variables Store information needed by the program Must have a TYPE
© A+ Computer Science - VARIABLES © A+ Computer Science -
Introduction to Java, and DrJava part 1
Expressions and Assignment
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Primitive Types and Expressions
Presentation transcript:

Basic Java Syntax Comments Basic data types Operators and assignment

Quick Comments // Works just like in pseudocode // From the double slash to the end of the // line becomes a comment (no semicolon) Also works like: a = b; // This is okay // more later...

Pseudocode Num –Could hold all types of numbers –Absolutely no rules No limits 0 to  No concerns about precision No concerns about accuracy 1/3 =

Java Two types of numbers –Whole numbers Generically known as integer type numbers –Fractional numbers Typically known as floating point numbers Why? –Whole numbers Exact Compact Fast Limited size –Fractional numbers Used for “real” world applications Much larger range Imprecise (?)

Characters Pseudocode –char was used to store a single character: ‘A’ –Could be used for sorting: ‘a’ < ‘b’ –No compatibility with numbers Java –char still holds a single character: ‘A’ –Can be used for sorting: ‘a’ < ‘b’ –Internally is actually stored as a number e.g. ‘a’ actually stored as 97 –Can do arithmetic! ‘a’ + 1 equals ‘b’

Booleans Pseudocode –TRUE/FALSE Java –true/false

Strings Pseudocode –World’s most fabulous built-in string type! –Very untypical –Historically was linked list of characters! –Too much work for too little benefit –Remember: Pseudocode can do anything!!! Java –We’ll defer Strings since they are implemented as Objects!

Pseudocode Complex Built-in User defined Atomic Num Char Boolean Ptr n/a Strings “Records”

Java Whole numbers –byte –short –int –long Fractional Numbers –float –double Characters –char Booleans –boolean Relax!!! We won’t use all of these Probably just... In case you’re curious, the next slide shows the technical details! Not important in CS 1311!!!In case you’re curious, the next slide shows the technical details! Not important in CS 1311!!! Watch capitalization!!!

Data Type Ranges TypeSizeMinDefault booleanfalse Max 1false*true* char'\u0000' (null)16 byte(byte) short(short) ,76832,767 int032 -2,147,483,6482,147,483,647 long0L64 -9,223,372,036,854,775,8089,223,372,036,854,775,807 float0.0F32 Approx ±3.4E+38 with 7 significant digits double0.0D64 Approx ±1.7E+308 with 15 significant digits void * Not truly min and max.

Java Complex Built-in User defined Primitives byte short int long float double char boolean n/a Strings plus lots more! “Classes”

Java Actually Objects! Built-in User defined Primitives byte short int long float double char boolean n/a Strings plus lots more! “Classes”

The Classic Duality Programming languages have always wrestled with the difference between assigning a value and the equality relational operator Equality (Boolean Result) –BASIC A = B –Pascal A = B –FORTRAN A.EQ. B –C A == B –Pseudocode A = B –Java A == B Equality (Boolean Result) –BASIC A = B –Pascal A = B –FORTRAN A.EQ. B –C A == B –Pseudocode A = B –Java A == B Assignment –BASIC LET A = B –Pascal A := B –FORTRAN A = B –C A = B –Pseudocode A  B –Java A = B Assignment –BASIC LET A = B –Pascal A := B –FORTRAN A = B –C A = B –Pseudocode A  B –Java A = B

Variable Declarations Pseudocode: – isoftype – e.g. Count isoftype Num Java: – ; – e.g. int count; or (optional initialization at declaration) – = ; – e.g. int count = 100; Equivalent to – int count; – count = 100;

Declaration Examples int Counter; int NumStudents = 583; double GPA; double BatAvg =.406; char Gender; char Gender = ‘f’; boolean Safe; boolean Empty = true;

Assignment Statements Pseudocode: – –e.g. Percent <- 100 * fraction Java: – = ; –e.g. Percent = 100 * fraction; Note: In Java, we distinguish this use of ‘=‘ from the equality test by using ‘==‘ to test for equality e.g. if( Percent == 50 )... Note: In Java, we distinguish this use of ‘=‘ from the equality test by using ‘==‘ to test for equality e.g. if( Percent == 50 )...

Assignment Examples Note that whole integers appearing in your source code are taken to be ‘ints’. So, you might wish to flag them when assigning to non-ints: double maxGrade = 100d; // now holds ‘100.0’ double temp = 583d; // holds double precision 583 double temp = 583. // Note decimal point float fTemp = 5.5; // ERROR! // Java thinks 5.5 is a double Upper and lower case letters can be used for ‘float’ (F or f), ‘double’ (D or d), and ‘long’ (l or L, but always use L): float maxGrade = 100F; // now holds ‘100.0’ long x = 583l; // holds 583, but looks like 5,381 long y = 583L; // Ah, much better!

Casting In Pseudocode we had a simple rule NO TYPE MISMATCHING!!! In other words: A <- B Required that A and B be the same type –Num, Char, Boolean, String, Ptr Most (if not all) real languages realize that it is often necessary to convert from one type to another. Two ways... –Do it automatically –Enforce typing rules

Casting In Java two things can happen when you type mismatch –The compiler will realize that you may be losing information and give you an error –The compiler may know how to make the conversion with no problem Example float f = 123.0F; double d = 123.0; f = d; // ERROR d = f; // No problem It is possible to override this behavior! f = (float)d; // Note: d is unchanged. Casting!

Casting It gets trickier! Suppose we want to divide two ints int a = 5; int b = 2; float f; f = a/b; f = ? But what if I want the decimals? One solution float fa = a; float fb = b; f = fa/fb; f = ? Or we could do: f = (float)a/(float)b; Note: f = (float)(a/b); // doesn’t solve problem...

Operators Arithmetic: +, -, *, /, % (mod), etc. Example: x = (a + b)/(c + d); Relational: >, =, <=, != instead of <> == instead of = Example: boolean quit = (index == 100); Boolean: AND becomes && OR becomes || NOT becomes ! if( !quit || (index < 5) ) { // do something } Note: these must be double: && ||