Variabel, Tipe Data & Operator Teguh Sutanto, M.Kom.

Slides:



Advertisements
Similar presentations
Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
Basic Java Programming Teguh Sutanto, M.Kom.. Tujuan Mahasiswa dapat menyebutkan berbagai tipe data dan operato.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Pert II: Basic Java Programming Teguh Sutanto, M.Kom.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
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.
Primitive Variables.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
ITEC 109 Lecture 7 Operations. Review Variables / Methods Functions Assignments –Purpose? –What provides the data? –What stores the data? –What type of.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
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.
Programming Principles Operators and Expressions.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
CSE 220 – C Programming Expressions.
Chapter 4 Assignment Statement
Elementary Programming
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Lecture 2: Data Types, Variables, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Multiple variables can be created in one declaration
Chapter 3 Assignment Statement
Chapter 2.
Starting JavaProgramming
Fundamentals 2.
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Variables and Constants
Presentation transcript:

Variabel, Tipe Data & Operator Teguh Sutanto, M.Kom.

Buku Hari ini

Menu Hari ini Review basic programing Variable Tipe Data Operator

Statement & Ekspression Java statements appear inside methods and classes; they describe all activities of a Java program. Expressions produce values; an expression is evaluated to produce a result that is to be used as part of another expression or in a statement.

Variable A variable is a named memory location capable of storing data of a specified type A variable must be declared before it can be used. A variable declaration specifies: the type of data that the variable can hold, for example int or double, and the name of the variable. When naming a variable, you should choose a name that is meaningful. A variable may be declared and given an initial value with a single initialization statement.

Declaring Variables must start with a letter (A to Z, a to z) or (unusually) an underscore ‘_’; can contain any number of letters or digits (a digit is 0 to 9); can contain the underscore ‘_’; can be up to 255 characters long. LegalIlegal namaDepan123Nama no_telpno telp min123%area

Constant A constant is a value that never changes In Java, a constant is a field with a particular set of modifiers: static and final. Most are also public, though it is possible (and sometimes useful) to have private, package, and protected constants.

Tipe Data Isi Data Text CharString Numerik Integer Floating Point Boolean boolean

Memasukan Data Dari Luar Program

Menggunakan class java.util.Scanner Include the import statement: import java.util.*; Declare a Scanner object as Scanner name = new Scanner(System.in) Methods name.nextLong() //membaca data dengan tipe long name.nextDouble() //membaca data dengan tipe double name.nextFloat() //membaca data dengan tipe float name.nextBoolean() //membaca data dengan tipe boolean name.nextInt() //membaca data dengan tipe int name.nextShort() //memcaca data dengan tipe short

Problem Statement Write an application that prompts for the number of pizzas and the number of toppings. The program should calculate the price of the pizza (including sales tax) and print a receipt.

Operator di Java KategoriOperator postfixexpr++ expr-- unary++expr --expr +expr -expr ~ ! casting(type) Multiplicative* / % additive+ - shift > >>> relational = instanceof

Operator di Java ( lanjutan …) KategoriOperator equality== != Bitwise AND& bitwise exclusive OR^ bitwise inclusive OR| Logical AND&& logical OR|| Ternary? : assignment= += -= *= /= %= &= ^= |= >= >>>=

OpertorNama OperatorKeterangan ++eksPrefix IncrementPenambanan 1 dari nilai ekspresi --eksPrefix DecrementPengurangan 1 dari nilai ekspresi +eksUnary plusMengindikasikan sebuah bilangan positive -eksUnary minusMembuat negative nilai ekspresi ~Bitwise complement Membalik nilai integer per bit !Logical complementMembalik true dan false

Multiplicative Operator multiplication (*) division (/) modulus (%).

Lat2: Celsius to Fahrenheit The temperature F in Fahrenheit equals (9 / 5) C +32 where C is the Celsius temperature. Write a program that computes and displays the temperatures in Fahrenheit for Celsius values 5, 0, 12, 68, 22.7, 100, and 6.

Lat 2: JamMenitDetik Buatlah program yang meminta user untuk memasukan jumlah detik, kemudian program akan menghitung berapa jam, berapa menit dan berapa detik. Diskusikan dengan kelompok anda, kemudian buatlah programnya

Lat 3:Uptime The uptime command of the UNIX operating system displays the number of days, hours, and minutes since the operating system was last started. For example, the UNIX command uptime might return the string Up 53 days 12:39 Write a program that converts the 53 days, 12 hours, and 39 seconds to the number of seconds that have elapsed since the operating system was last started. Catatan masukan variable: totalHari totalJam totalMenit

Casting Software developers often find that they need a variable of one type to be a variable of another type. In Java (and most programming languages), you can't change the type of a variable. Instead, you should create a new variable and convert the existing variable into the new variable's type. That process is called casting, and Java provides an operator of sorts for doing it. I say, “of sorts,” because the casting operator differs according to the data type to which you're casting.

Relational Operators The relational operators compare things to one another. In particular, they determine whether one value is greater than, less than, equal to, or not equal to another value. For the relational operators to work, the items being compared have to be comparable. > (greater than), = (greater than or equal to), and <= (less than or equal to) all work on primitives, but they don't work on objects.

RelationOprDemo.java

Equality Operators Java has two equality operators: == (equals) != (not equals).

Bitwise AND Operator (&) The bitwise AND operator (&) compares two binary values and sets each bit to 1 where each bit in the two values being compared is 1

Bitwise AND VariableDesimalBinary bil bil result

Bitwise Inclusive OR Operator (|) The bitwise Inclusive OR operator (|) compares two binary values and sets each bit to 1 if either bit is 1

Bitwise OR VariableDesimalBinary bil bil result

Bitwise Exclusive OR Operator (^) The bitwise Exclusive OR (often shortened to XOR) operator (^) compares two binary values and sets each bit to 1 if the bits differ.

Bitwise XOR VariableDesimalBinary bil bil result

Logical AND Operator (&&) The logical AND operator (&&) returns true if both arguments are true and false if either one is false

Logical OR Operator (||) The logical AND operator (||) returns true if either of its arguments are true and false only if both arguments are false.

Assignment Operators The assignment operators set values and assign object references. The basic assignment operator (=) is by far the most often used operator Every time we put something like int = 0 (assigning a value to aprimitive) or Date now = new Date() (assigning an object reference to a variable) into an example, we use the basic assignment operator.

OperatorDescription = This is the basic assignment operator. += Add the right-hand operand to the left-hand operand and assign the result to the left-hand operand. x += 2 is the same as x = x = Subtract the right-hand operand from the left-hand operand and assign the result to the lefthand operand. x -= 2 is the same as x = x – 2. *= Multiply the right-hand operand by the left-hand operand and assign the result to the lefthand operand. x *= 2 is the same as x = x * 2. /= Divide the right-hand operand by the left-hand operand and assign the result to the left-hand operand. x /= 2 is the same as x = x / 2. %= Use the right-hand operand to determine the modulus of the left-hand operator and assign the result to the left-hand operator. x %= 2 is the same as x = x % 2.

OperatorDescription &= Perform a bitwise and operation and assign the result to the left-hand operator. x &= y is the same as x = x & y. |= Perform a bitwise inclusive operation and assign the result to the left-hand operator. x |= y is the same as x = x | y. ^= Perform a bitwise exxclusive or operation and assign the result to the left-hand operator. x ^= y is the same as x = x ^ y. <<= Perform a bitwise left-shift operation and assign the result to the left-hand operator. x <<= y is the same as x = x << y. >>= Perform a bitwise right-shift operation and assign the result to the left-hand operator. x > y. >>>= Perform a bitwise signed right-shift operation and assign the result to the left- hand operator. x >>>= y is the same as x = x >>> y