Java Structure import java_packages; class JavaClass { ------ member variables declarations; ------ void otherMethod( ) { ------ } public static void main(String[]

Slides:



Advertisements
Similar presentations
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.
Advertisements

Constants and Data Types Constants Data Types Reading for this class: L&L,
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
JAVA PROGRAMING LANGUAGE. Content of Java 2 SDK  Development Tools (In the bin subdirectory.) Tools and utilities that will help you develop, execute,
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
Java Syntax Primitive data types Operators Control statements.
Some basic I/O.
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.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Review Java.
JavaScript, Third Edition
String Escape Sequences
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Friday, 24 October ICT:: lecture 2 Introduction to Programming.
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.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
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.
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.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Sachin Malhotra Saurabh Choudhary
Multiple variables can be created in one declaration
Java Programming Language
Java Basics Data Types in Java.
Names of variables, functions, classes
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[] args) { local variables declarations; }

Java Structure Example: public class BasicsDemo { public static void main(String[] args) { int test1 = 10; int test2; test1 += 5; test2 = test1 + 15; System.out.println(“Test 1 = " + test1); System.out.println(“Test 2 = " + test2); }

Variables Definition: A variable is an item of data named by an identifier. –You must explicitly provide a name and a type for each variable you want to use in your program. –The variable's name must be a legal indentifier An unlimited series of Unicode characters that begins with a letter. –You use the variable name to refer to the data that the variable contains. –The variable's type determines what values it can hold and what operations can be performed on it.

Variables Declaration A variable declaration: var_type var_name = var_initialization ; Example: MaxVarDemo.java

Data Types Every variable must have a data type. A variable's data type determines the values that the variable can contain and the operations that can be performed on it. The Java programming language has two categories of data types: –Primitive –Reference A variable of primitive type contains a single value of the appropriate size and format for its type: a number, a character, or a boolean. The value of a reference type variable is a reference to (an address of) the value or set of values represented by the variable: an arrays, a class, and an interface.

Primitive Data Types KeywordDescriptionSize/Format (integers) byteByte-length integer8-bit two's complement shortShort integer16-bit two's complement intInteger32-bit two's complement longLong integer64-bit two's complement (real numbers) float Single-precision floating point 32-bit IEEE 754 double Double-precision floating point 64-bit IEEE 754 (other types) charA single character16-bit Unicode character boolean A boolean value (true or false) true or false

Literal Values A literal primitive value can directly put in the code. Example:int anInt = 4; The digit 4 is a literal integer value. LiteralData Type 178 int 8864L long double D double Ffloat 26.77e3 double ' c '' c ' char trueboolean falseboolean

Variable Name In the Java programming language, the following must hold true for a simple name: –It must be a legal identifier. An identifier is an unlimited series of Unicode characters that begins with a letter. –It must not be a keyword, a boolean literal ( true or false ), or the reserved word null. –It must be unique within its scope. A variable may have the same name as a variable whose declaration appears in a different scope. By Convention : –Variable & Method names begin with a lowercase letter –Class name begin with an uppercase letter. –If a variable, method or class names consist of more than one word, the words are joined together, and each word after the first begins with an uppercase letter, like this: isVisible.

Variable Scope The location of the variable declaration within your program establishes its scope and places it into one of these four categories: –member variable –local variable –method parameter –exception-handler parameter

Variable Scope Member (Instance) Variable: A member variable is a member of a class or an object. It is declared within a class but outside of any method or constructor. A member variable's scope is the entire class. Local Variable: Local variables are declared within a block of code. In general, the scope of a local variable extends from its declaration to the end of the code block in which it was declared. Consider the following code sample: if (...) { int i = 17;... } System.out.println("The value of i = " + i); // error The final line won't compile because the local variable i is out of scope.

Variable Scope Method Parameter: Parameters are formal arguments to methods and are used to pass values into methods. The scope of a parameter is the entire method for which it is a parameter. Exception-handler Parameter: Exception-handler parameters are similar to parameters but are arguments to an exception handler rather than to a method. The scope of an exception-handler parameter is the code block between { and } that follow a catch statement.

Variable Initialization Local variables and member variables can be initialized with an assignment statement when they're declared. The data type of the variable must match the data type of the value assigned to it. Examples: // integers byte byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; // real numbers float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE; // other primitive types char aChar = 'S'; boolean aBoolean = true;

Final Variable The value of a final variable cannot change after it has been initialized. Such variables are similar to constants in other programming languages. To declare a final variable, use the final keyword in the variable declaration before the type: final int aFinalVar = 0; The previous statement declares a final variable and initializes it, all at once. Subsequent attempts to assign a value to aFinalVar result in a compiler error.

Final Variable A final local variable that has been declared but not yet initialized is called a blank final. final int blankfinal;... blankfinal = 0;... Once a final local variable has been initialized, it cannot be set again. Any later attempts to assign a value to blankfinal result in a compile-time error.

Operators An operator performs a function on one, two, or three operands. An operator that requires one operand is called a unary operator. operator operand //prefix notation operand operator //postfix notation An operator that requires two operands is a binary operator. operand1 operator operand2 //infix notation An operator that requires two operands is a ternary operator. operand1 ? operand2 : operand3 //infix notation

Arithmetic Operators Example: ArithmeticDemo.java Binary Operator:

Arithmetic Operators Example: ArithmeticDemo2.java Unary Operator:

Relational Operators Example: RelationalDemo.java A relational operator compares two values and determines the relationship between them.

Conditional Operator Relational operators often are used with conditional operators to construct more complex decision- making expressions. Conditional Operators: Example: ConditionalDemo.java

Shift Operators A shift operator performs bit manipulation on data by shifting the bits of its first operand right or left. Example: 13 >> 1  6 The binary representation of the number 13 is The result of the shift operation is 1101 shifted to the right by one position  110, or 6 in decimal. The left-hand bits are filled with 0’s as needed.

Logical (Bitwise) Operators

Example: 13 & 12  & 12  & 12  1 The binary representation of the number 13 is The binary representation of the number 13 is    13 &1100  12 | 1100  12 ^ 1100     1 Example Program: ShiftLogicalDemo.java

Assignment Operators

Other Operators Shortcut if-else Statement The ?: operator is a conditional operator that is short-hand for an if - else statement: op1 ? op2 : op3 The ?: operator returns op2 if op1 is true or returns op3 if op1 is false. The [ ] Operator Square brackets are used to declare arrays, to create arrays, and to access a particular element in an array. Here's an example of an array declaration: float[] arrayOfFloats = new float[10]; The previous code declares an array that can hold ten floating point numbers. Here's an example to access the 7 th element of array : arrayOfFloats[6];

Other Operators The. Operator The dot (.) operator accesses instance members of an object or class members of a class. The () Operator When declaring or calling a method, the list of the method's arguments are put between ( and ). The (data type) Operator Casts (or "converts") a value to the specified type. Example: …… int i; float j = 10.5; i = (int) j + 11; ……