Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.

Slides:



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

Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
CMT Programming Software Applications
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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.
1 Chapter 2: Elementary Programming Shahriar Hossain.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
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.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Chapter 2 Elementary Programming
Primitive Variables.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
 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.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Primitive Variables.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
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.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
String class.
Yanal Alahmad Java Workshop Yanal Alahmad
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Advanced Programming Behnam Hatami Fall 2017.
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Chapter 2: Java Fundamentals
elementary programming
In this class, we will cover:
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Chapter 2: Java Fundamentals cont’d
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Java Overview

Comments in a Java Program 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 for an HTML page goes here */

Identifiers Can be a series of characters consisting of letters, digits, underscores(_) and dollar signs ($). It does not begin with a digit and does not contain spaces and cannot be a reserved word. Examples: WelcomeApplet, $value, _value, my_inputField, button8 Invalid Examples: 8button or input field

Identifier Conventions Class names begin with a capital letter and have a capital letter for every word in the class name ( Ex: WelcomeAppletClass) Variables and Methods begin with lower case letter and have a capital letter for every word in the variable ( Ex: firstNumber, and myFirstMethod) Constants are all caps with an underscore separating individual words ( Ex: RATE_PER_HOUR)

Primitive Data Types and Declaring Variables Integer values (32 bits from to ): 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), short (16 bits) and long (64 bits) byte verySmallInteger; short numberInClass; long veryBigInteger; Other real values: double (64 bits) double nationalDebt; –By default, real values are double. For example, 5.0 is considered to be double not float. To make it float, write it as 5.0f

Number Literals int i = 34; long l = ; float f = 100.2f; or float f = 100.2F; double d = 100.2; or double d = 100.2D; or Double d=100.2d;

Primitive Data Types Continued Character data –uses ISO Unicode Character set (16 bits) Established by the Unicode Consortium to support the interchange, processing, and display of texts for the world’s diverse languages. (see –Ranges from ‘\u0000’ to ‘\uFFFF’ –ASCII is a subset of Unicode (‘\u0000’ to ‘\u007F’ corresponds 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 = ; static final int SIZE = 3;

Shortcut Operators OperatorExampleEquivalent +=i+=8i = i+8 -=f-=8.0f = f-8.0 *=i*=8i = i*8 /=i/=8i = i/8 %=i%=8i = i%8

Increment and Decrement Operators x = 1; y = 1 + x++; y = x; y = 1 + x--; y = x;

Operator Precedence Casting ++, -- *, /, % +, -, => ==, !=; && || =, +=, -=, *=, /=, %=

Numeric Type Conversion Consider the following statements: byte i = 100; long l = i*3+4; double f = i*3.1+l/2; When performing 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

Numeric Type Conversion 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 performed automatically. Narrowing a type must be performed explicitly.

Numeric Type Conversion Examples: float f = 10.1; //illegal float f = 10.1f; //legal float f = (float)10.1; //explicit casting

The String Class java.lang.String is a class that models a sequence of characters as a string. The String class contains 11 constructors 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 constructors –String () –String (String s)

Strings Instantiating a string object –String name = “Joe”; –String name = new String (“Joe”);

String Methods char charAt (int position) –Return the character located at the specified position char c = name.charAt(2); equals –if (s1.equals(s2))

String Methods 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

String Methods 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

String Methods 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 for which the method is called boolean equalsIgnoreCase (Object o)

String Methods 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

String Methods indexOf and lastIndexOf – searches for a specified character or 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 for ‘a’ i contains 6 –int i=lastIndexOf (‘b’); //i=7 –int i=letters.indexOf(“def”); // i=3

String Methods 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 or end