Comments in a Java Program. Java Overview. Identifiers. Identifier Conventions. Primitive Data Types and Declaring Variables

Similar documents
Chapter 2 Elementary Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming with Java

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Basics of Java Programming

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

ECE 122 Engineering Problem Solving with Java

Computational Expression

Datatypes, Variables, and Operations

Values and Variables 1 / 30

Java Notes. 10th ICSE. Saravanan Ganesh

Chapter 2 Primitive Data Types and Operations. Objectives

3. Java - Language Constructs I

Declaration and Memory

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter 2: Data and Expressions

Chapter 2 ELEMENTARY PROGRAMMING

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

A variable is a name for a location in memory A variable must be declared

Java Fall 2018 Margaret Reid-Miller

Chapter 2: Data and Expressions

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

CMPT 125: Lecture 3 Data and Expressions

Chapter 2: Data and Expressions

Full file at

CS 1301 Ch 8, Part A

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Important Java terminology

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CS-201 Introduction to Programming with Java

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

JAVA Programming Concepts

Chapter 2 Primitive Data Types and Operations

Visual C# Instructor s Manual Table of Contents

Values, Variables, Types & Arithmetic Expressions. Agenda

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 2 Primitive Data Types and Operations

Chapter 2: Basic Elements of Java

Lecture Set 4: More About Methods and More About Operators

Java Basic Datatypees

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Getting started with Java

Java enum, casts, and others (Select portions of Chapters 4 & 5)

CSEN 202 Introduction to Computer Programming

Advanced Computer Programming

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CEN 414 Java Programming

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Chapter 4 Mathematical Functions, Characters, and Strings

Chapter 2: Using Data

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Chapter 1 Introduction to java

CST242 Strings and Characters Page 1

Programming with Java

JAVA Programming Fundamentals

Data Conversion & Scanner Class

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Chapter 2: Using Data

Creating Strings. String Length

Lecture Set 4: More About Methods and More About Operators

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

USING LIBRARY CLASSES

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Reserved Words and Identifiers

Fall 2017 CISC124 9/16/2017

Java Primer 1: Types, Classes and Operators

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Number Representation & Conversion

Language Reference Manual

More Programming Constructs -- Introduction

Example: Tax year 2000

COMP Primitive and Class Types. Yi Hong May 14, 2015

AP Computer Science A

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

Introduction to Programming Using Java (98-388)

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives

Chapter 6 Primitive types

Zheng-Liang Lu Java Programming 45 / 79

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 2 Elementary Programming

COMP6700/2140 Data and Types

ESc101 : Fundamental of Computing

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

CS110: PROGRAMMING LANGUAGE I

Variables, Constants, and Data Types

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

Basic Computation. Chapter 2

Transcription:

Comments in a Java Program Java Overview Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over multiple lines like C Example: /* This is a multiple line comment. */ Comments can be special Java comments that help produce java documentation using the javadoc utitlity program. Example: /** comments f an HTML page goes here */ Identifiers Can be a series of characters consisting of letters, digits, undersces(_) and dollar signs ($). It does not begin with a digit and does not contain spaces and cannot be a reserved wd. Examples: WelcomeApplet, $value, _value, my_inputfield, button8 Invalid Examples: 8button input field Identifier Conventions Class names begin with a capital letter and have a capital letter f every wd in the class name ( Ex: WelcomeAppletClass) Variables and Methods begin with lower case letter and have a capital letter f every wd in the variable ( Ex: firstnumber, and myfirstmethod) Constants are all caps with an undersce separating individual wds ( Ex: RATE_PER_HOUR) Primitive Data Types and Declaring Variables Integer values (32 bits from -2 31 to 2 31-1): int number1, number2; Real values (32 bits): float area; True/False values (8 bits): boolean done=true; Primitive Data Types Continued Other Integers: byte (8 bits), sht (16 bits) and long (64 bits) byte verysmallinteger; sht numberinclass; long verybiginteger; Other real values: double (64 bits) double nationaldebt; By default, real values are double. F example, 5.0 is considered to be double not float. To make it float, write it as 5.0f 1

int i = 34; long l = 1000000; float f = 100.2f; float f = 100.2F; double d = 100.2; double d = 100.2D; Double d=100.2d; Number Literals Primitive Data Types Continued Character data uses ISO Unicode Character set (16 bits) Established by the Unicode Constium to suppt the interchange, processing, and display of texts f the wld s diverse languages. (see www.unicode.g) Ranges from \u0000 to \uffff ASCII is a subset of Unicode ( \u0000 to \u007f cresponds to the 128 ASCII characters Examples: char letter='a'; //A char letter='\u0041'; //A char letterpi='\03a0'; // Constants static final datatype CONSTANT_NAME = valueofconstant; static final double PI = 3.14159; static final int SIZE = 3; Shtcut Operats Operat Example Equivalent += i+=8 i = i+8 -= f-=8.0 f = f-8.0 *= i*=8 i = i*8 /= i/=8 i = i/8 %= i%=8 i = i%8 Increment and Decrement Operats x = 1; y = 1 + x++; y = 1 + ++x; y = 1 + x--; y = 1 + --x; Casting Operat Precedence ++, -- *, /, % +, - <, <=, >, => ==,!=; && =, +=, -=, *=, /=, %= 2

Consider the following statements: byte i = 100; long l = i*3+4; double f = i*3.1+l/2; When perfming a binary operation involving operands of different types, Java automatically converts the operand of a smaller range type to a larger range type of the other operand. Example: If one is int and the other is float, the int is converted to float Casting is an operation that converts a value of one data type into a value of another data type. Casting a variable of a type with a small range to a variable with a larger range is known as widening a type. Casting a variable of a type with a large range to a variable with a smaller range is known as narrowing a type. Widening a type can be perfmed automatically. Narrowing a type must be perfmed explicitly. The String Class Examples: float f = 10.1; //illegal float f = 10.1f; //legal float f = (float)10.1; //explicit casting java.lang.string is a class that models a sequence of characters as a string. The String class contains 11 constructs and > 40 methods Examples: String message = Welcome to Java! ; String class String sequence of character data String objects are immutable they cannot be changed once they have been created String class has 11 constructs String () String (String s) Strings Instantiating a string object String name = Joe ; String name = new String ( Joe ); 3

char charat (int position) Return the character located at the specified position char c = name.charat(2); equals if (s1.equals(s2)) int length() Returns the number of characters in the string int l = name.length(); String substring (int start) String substring (int start, int end) Used to extract a substring from a larger string start is the position of the first character to extract end is NOT the position of the last character to extract but the position following the last character to extract Assume: String letters = abcdef ; System.out.println (letters.substring (3)); Prints def System.out.println(letters.substring (0,3)); Prints abc System.out.println (letters.substring (2,4)); Prints cd boolean equals (Object o) Return true if the object is a string that has the same length and contains the same characters as the string f which the method is called boolean equalsignecase (Object o) int compareto(string s) Returns 0 if both strings are the same Returns a positive number if the argument string comes first Returns a negative number if the calling string come first indexof and lastindexof searches f a specified character substring in a String Example: String letters = abcdefabcd ; int i=letters.indexof( c ); // i contains 2 int i=letters.indexof( $ ); // i contains -1 int i=letters.indexof ( a,1); //starts with 1 and searches f a i contains 6 int i=lastindexof ( b ); //i=7 int i=letters.indexof( def ); // i=3 4

replace (char,char), touppercase(), trim() String s1 = hello ; s1 = s1.replace ( l, L ); //returns a string object in which every occurrence of l is replaced by L s1 = s1.touppercase(); // HELLO s1 = s1.trim() //remove any white spaces at beginning end 5