Review of Java Fundamentals

Slides:



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

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Chapter 1 Review of Java Fundamentals Lecture 2 Jenny Walter Fall 2008.
VB .NET Programming Fundamentals
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.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 2: Using Data.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
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.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
© 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
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.
© 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.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Java Primer 1: Types, Classes and Operators
Tokens in C Keywords Identifiers Constants
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Lectures on Numerical Methods
elementary programming
The Data Element.
An Introduction to Programming with C++ Fifth Edition
Primitive Types and Expressions
The Data Element.
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Review of Java Fundamentals Chapter 1 Review of Java Fundamentals © 2011 Pearson Addison-Wesley. All rights reserved

Language Basics Java application Collection of classes One class contains the main method Java programs can also be written as applets © 2011 Pearson Addison-Wesley. All rights reserved

Setting up the Environment JDK(Java Development Kit) : JDK is intended for software developers and includes development tools such as the Java compiler, Javadoc, Jar, and a debugger. JRE(Java Runtime Environment) : JRE contains the parts of the Java libraries required to run Java programs and is intended for end users. JRE can be view as a subset of JDK. JVM: JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms.  © 2011 Pearson Addison-Wesley. All rights reserved

Comments Comment line Multiple-line comment javadoc comments Begins with two slashes (//) Continues until the end of the line Multiple-line comment Begins with /* and ends with */ Useful for debugging Cannot contain another multiple-line comment javadoc comments Begins with /** and ends with */ © 2011 Pearson Addison-Wesley. All rights reserved

Examples © 2011 Pearson Addison-Wesley. All rights reserved

Identifiers and Keywords Sequence of letters, digits, underscores, and dollar signs Must begin with either a letter or underscore Used to name various parts of the program Java distinguishes between uppercase and lowercase letters Keywords Java reserved identifiers © 2011 Pearson Addison-Wesley. All rights reserved

© 2011 Pearson Addison-Wesley. All rights reserved

© 2011 Pearson Addison-Wesley. All rights reserved

Variables Represents a memory location Contains a value of primitive type or a reference Its name is a Java identifier Declared by preceding variable name with data type double radius; // radius of a sphere String name; // reference to a String object © 2011 Pearson Addison-Wesley. All rights reserved

Primitive Data Types Organized into four categories Boolean Character Integer Floating point Character and integer types are called integral types Integral and floating-point types are called arithmetic types © 2011 Pearson Addison-Wesley. All rights reserved

References Data type used to locate an object Java does not allow programmer to perform operations on the reference value Location of object in memory can be assigned to a reference variable © 2011 Pearson Addison-Wesley. All rights reserved

© 2011 Pearson Addison-Wesley. All rights reserved

Literal Constants Indicate particular values within a program Used to initialize the value of a variable Decimal integer constants Do not use commas, decimal points, or leading zeros Default data type is either int or long Floating constants Written using decimal points Default data type is double © 2011 Pearson Addison-Wesley. All rights reserved

Literal Constants Character constants Enclosed in single quotes Default data type is char Literal character strings Sequence of characters enclosed in double quotes © 2011 Pearson Addison-Wesley. All rights reserved

Named Constants Have values that do not change Declared as a variable but using the keyword final © 2011 Pearson Addison-Wesley. All rights reserved

Assignments and Expressions Combination of variables, constants, operators, and parentheses Assignment statement Example: radius = r; Arithmetic expression Combine variables and constants with arithmetic operators and parentheses Arithmetic operators: *, /, %, +, - © 2011 Pearson Addison-Wesley. All rights reserved

Assignments and Expressions Relational expressions Combine variables and constants with relational, or comparison, and equality operators and parentheses Relational or comparison operators: <, <=, >=. > Equality operators: ==, != Evaluate to true or false © 2011 Pearson Addison-Wesley. All rights reserved

Assignments and Expressions Logical expressions Combine variables and constants of arithmetic types, relational expressions with logical operators Logical operators: &&, || Evaluate to true or false Short-circuit evaluation Evaluates logical expressions from left to right Stops as soon as the value of expression is apparent © 2011 Pearson Addison-Wesley. All rights reserved

Assignments and Expressions Implicit type conversions Occur during assignment and during expression evaluation Right-hand side of assignment operator is converted to data type of item on left-hand side Floating-point values are truncated not rounded Integral promotion Values of type byte, char, or short are converted to int Conversion hierarchy int → long → float → double © 2011 Pearson Addison-Wesley. All rights reserved

Assignments and Expressions Explicit type conversions Possible by means of a cast Cast operator Unary operator Formed by enclosing the desired data type within parentheses Multiple assignments Embed assignment expressions within assignment expressions Example: a = 5 + (b = 4) Evaluates to 9 while b is assigned 4 © 2011 Pearson Addison-Wesley. All rights reserved

Assignments and Expressions Other assignment operators -= *= /= %= ++ -- Useful Java Cheat sheet: https://introcs.cs.princeton.edu/java/11cheatsheet/ © 2011 Pearson Addison-Wesley. All rights reserved

Arrays Collection of elements with the same data type Array elements have an order Support direct and random access One-dimensional arrays Declaration example final int DAYS_PER_WEEK = 7; double [] maxTemps = new double[DAYS_PER_WEEK]; Length of an array is accessible using data field length Use an index or subscript to access an array element © 2011 Pearson Addison-Wesley. All rights reserved

Arrays Figure 1-7 One-dimensional array of at most seven elements © 2011 Pearson Addison-Wesley. All rights reserved

Arrays One-dimensional arrays (continued) Multidimensional arrays Initializer list example double [] weekDayTemps = {82.0, 71.5, 61.8, 75.0, 88.3}; You can also declare array of object references Multidimensional arrays Use more than one index Declaration example final int DAYS_PER_WEEK = 7; final int WEEKS_PER_YEAR = 52; double[][] minTemps = new double[DAYS_PER_WEEK][WEEKS_PER_YEAR]; © 2011 Pearson Addison-Wesley. All rights reserved

Arrays Figure 1-8 A two-dimensional array © 2011 Pearson Addison-Wesley. All rights reserved

Arrays Passing an array to a method Declare the method as follows: public double averageTemp(double[] temps, int n) Invoke the method by writing: double avg = averageTemp(maxTemps, 6); Location of array is passed to the method Cannot return a new array through this value Method can modify content of the array © 2011 Pearson Addison-Wesley. All rights reserved