Java Primer I CMSC 202

Similar documents
Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Chapter 2: Elements of Java

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Variables, Constants, and Data Types

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Java Interview Questions and Answers

Moving from CS 61A Scheme to CS 61B Java

Java Crash Course Part I

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Lecture 1 Introduction to Java

Pemrograman Dasar. Basic Elements Of Java

Conditionals (with solutions)

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Example of a Java program

PL / SQL Basics. Chapter 3

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

J a v a Quiz (Unit 3, Test 0 Practice)

Lecture 5: Java Fundamentals III

Introduction to Java

CS 106 Introduction to Computer Science I

Chapter 2 Elementary Programming

Objective-C Tutorial

JavaScript: Control Statements I

Sources: On the Web: Slides will be available on:

arrays C Programming Language - Arrays

Java Basics: Data Types, Variables, and Loops

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Introduction to Java. CS 3: Computer Programming in Java

Arrays in Java. Working with Arrays

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

In this Chapter you ll learn:

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Introduction to Python

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Statements and Control Flow

Chapter One Introduction to Programming

C++ Language Tutorial

VB.NET Programming Fundamentals

Informatica e Sistemi in Tempo Reale

Introduction to Visual C++.NET Programming. Using.NET Environment

AP Computer Science Java Subset

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Section of DBMS Selection & Evaluation Questionnaire

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Chapter 2 Introduction to Java programming

Object Oriented Software Design

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Java Classes. GEEN163 Introduction to Computer Programming

Introduction to Python

Object Oriented Software Design

6.087 Lecture 2 January 12, 2010

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Introduction to Data Structures

6.170 Tutorial 3 - Ruby Basics

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Install Java Development Kit (JDK) 1.8

Computer Programming Tutorial

Writing Control Structures

DATA STRUCTURES USING C

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

Oracle Internal & Oracle Academy

A Brief Introduction to MySQL

The C Programming Language course syllabus associate level

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Basic Programming and PC Skills: Basic Programming and PC Skills:

Introduction to Java Lecture Notes. Ryan Dougherty

Instruction Set Architecture (ISA)

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

SQL Server An Overview

Programming and Data Structures with Java and JUnit. Rick Mercer

Moving from C++ to VBA

Programming Languages CIS 443

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

AppendixA1A1. Java Language Coding Guidelines. A1.1 Introduction

Java CPD (I) Frans Coenen Department of Computer Science

I PUC - Computer Science. Practical s Syllabus. Contents

Object Oriented Software Design II

Chapter 3 Operators and Control Flow

3.GETTING STARTED WITH ORACLE8i

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Chapter 7D The Java Virtual Machine

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

El Dorado Union High School District Educational Services

Financial Data Access with SQL, Excel & VBA

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

An Incomplete C++ Primer. University of Wyoming MA 5310

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Chapter 5 Names, Bindings, Type Checking, and Scopes

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Python Lists and Loops

C++ Essentials. Sharam Hekmat PragSoft Corporation

Transcription:

Java Primer I CMSC 202

Variable Declaration Syntax: <type> <legal identifier>; Examples: int sum; float average; double grade = 98; Semicolon required! Must be declared before being used Must appear within a class declaration (no globals ) Must be declared of a given type (e.g. int, float, char, etc.) 2

Java's Legal Identifiers Are case-sensitive Cat, CAT, CaT are all different variable names Typically consist of letters, numbers and underscores Must not begin with a number Must not contain whitespace Must not be a reserved/key word 3

Naming Conventions Naming Conventions Additional rules that restrict the names of variables resulting in improving consistency/readability Most places of work and education have a set of naming conventions These are not language or compiler enforced CMSC 202 Naming Conventions Variables & methods Start with a lowercase letter Indicate word boundaries with an uppercase letter Restrict the remaining characters to digits and lowercase letters Classes Start with an uppercase letter Otherwise same as variables and methods See the CMSC 202 course website 4

Variable Types Primitive Type Declared to be of basic type e.g. float, double, char, int Variables hold actual data int x = 25; Reference Type Declared to be of class type e.g. String, MyClass, Integer Variables hold addresses to dynamically allocated memory space We will discuss this in more detail later String name = "Bubba"; 25 FF00 Bubba x name 5

Primitive Types Copyright 2008 Pearson Addison-Wesley. All rights reserved 6

Primitive Types All primitive type variables store the information inside of the variable int x = 25; x contains the value 25 There are no additional steps required to access the contents of x Default Values Java automatically initializes all declared primitive variables to a default value that is equivalent to 0. Integer and floating point types are set to 0. The character type is set to the \u0000 Unicode character (null). The boolean type is set to false. 7

Reference Types Reference type variables must be created dynamically and are generally in the form ReferencedType name = new ReferencedType(); The new keyword creates an instance of a class. It returns an address to the newly created object on the heap. Typically the address is assigned into a variable (e.g. name ). The instance can then be referenced using the variable name. Members and methods can be accessed using dot notation. name FF00 Referenced Object 8

Arrays Arrays are referenced objects that hold a fixed number of homogeneous data (i.e. data of the same type). These elements appear in contiguous memory. General form: Sample declarations: <type>[] <variable name>; int[] scores; float[] grades; What does each variable contain at this point? scores grades 9

Arrays Initializing an array requires the usage of the keyword new to create the space on the heap to hold the elements type[] variable_name = new type[number_of_elements]; int[] scores = new int[8]; scores FFAA Java initializes all elements of the array to the default value for that type The size of an array can be obtained by accessing the length member variable (e.g. scores.length). An array of size 8 will have what for indices? 10

scores Arrays FFAA 5 99 2 We can access any element in the array using array_name[index] scores[1] will return what value? scores[0] = 82; Assigns 82 to index 0 of the array How does accessing with array_name [index] really work? FFAA is the address of the first element of the array. Since all elements of an array are of a common type, we know that each element will consume the same amount of space. Using that knowledge, we can compute the location (offset) of the element within the array. scores[2] FFAA + size of (type)*index Luckily, Java handles all this for you! 11

Multi-Dimensional Arrays Really should be considered an array of arrays (and potentially of arrays, and so forth) You can declare multi-dimensional arrays just like single dimensional arrays. The general form: type[][] array_name = new type[ rows ][ columns ]; Example: char [][] tictactoeboard = new char[3][3]; Use the same access syntax as single dimensional arrays. What statement will place an O in the upper right corner? X 12

Printing to the Screen Formatted output System.out.printf("Printing integer %d%n",5); System.out.printf("%d %c %d", 1, 'a', 2); Place holders can be added to represent variables to be output in the format string. %d, %c, %f, %s What does each stand for? Every place holder that appears inside the output string must have a matching value separated by a comma. Add proceeding white space characters and precision to variables printed. System.out.printf("2 points of precision %10.2d", 89.999); Two points of precision 90.00 no newline character Other special formatting %n platform independent newline character \t horizontal tab 13

Printing to the Screen (con t) Unformatted output General formats: System.out.print( ) leaves cursor on same line System.out.println( ) cursor moves to next line Example: System.out.print( Hello ); System.out.print( there ); System.out.println( Hello ); System.out.println( there ); Output: Hello therehello there

Binary Operators What is a binary operator? An operator that has two operands <operand> <operator> <operand> Arithmetic Operators + - * / % Relational Operators < > == <= >= Logical Operators && 15

Relational Operators In Java, all relational operators evaluate to a boolean value of either true or false. x = 5; y = 6; x > y will always evaluate to false. Java has a ternary operator the general form is: (conditional expression)? true case : false case ; For example: System.out.println(( x > y )? "X is greater" : "Y is greater"); 16

Unary Operators Unary operators only have one operand.! ++ -- ++ and -- are the increment and decrement operators x++ a post-increment (postfix) operation ++x a pre-increment (prefix) operation What is the difference between these segments? x = 5; System.out.printf("x's value %d%n", x++); x = 5; System.out.printf("x's value %d%n", ++x); 17

Precedence Order of operator application to operands: Postfix operators: ++ -- (right to left) Unary operators: + - ++ --! (right to left) * / % (left to right) + - (left to right) < > <= >= ==!= &&? : Assignment operator: = (right to left) 18

A Sample Java Program Copyright 2008 Pearson Addison-Wesley. All rights reserved 19