Java Basics. Java High-level language  More readable for humans  Need to be translated to machine language for execution Compilers  CPU-independent.

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
CMT Programming Software Applications
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
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.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Hello, world! Dissect HelloWorld.java Compile it Run it.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Basic Elements of C++ Chapter 2.
COMPUTER SCIENCE I C++ INTRODUCTION
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
CS107 Introduction to Computer Science Java Basics.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction to Computer Systems and the Java Programming Language.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
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.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e 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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Primitive Data Types. Identifiers What word does it sound like?
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
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.
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Basic Computation
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Basic Elements of C++.
Data types and variables
JavaScript.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Basic Elements of C++ Chapter 2.
Chapter 2.
Introduction to Programming in Java
Building Java Programs Chapter 2
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Java Basics.
Introduction to Java Brief history of Java Sample Java Program
Chapter 2 Programming Basics.
Primitive Types and Expressions
Unit 3: Variables in Java
Variables and Constants
Presentation transcript:

Java Basics

Java High-level language  More readable for humans  Need to be translated to machine language for execution Compilers  CPU-independent  translation can target different CPUs (machine languages) Designed by Sun Microsystems in 1995  Sun was bought by Oracle in 2010 Designed with internet in mind  Can run in a web browser

Storing Data To store data  we need to allocate space in the memory  Declare (specify) Type what kind of data Name we can refer to it later Essentially a named location in the memory

Types int  (signed) integer double  double-precision floating point number boolean  true or false char  character

Names (“Identifiers”) Starts with a letter After that, can include  letter  digit Can these be names?  numberOfStudents  five5  55  5five Case sensitive  Balance and balance are different names Meaningful names improve readability  reduce mistakes

The Famous/Weird Semicolon Semicolon  Is similar to a period after a sentence in English  End of one instruction  Period is used to mean something else in Java Allocating space (“declaration”): int numberOfStudents; double temperature, humidity, pressure; boolean sunny, hurricane; char letterGrade; They are usually called variables similar to math  How do we vary/change the value?

Changing Values Assignment  =  Equal sign, but doesn’t mean equal as in math x = 97.5;  Means assign 97.5 to x (or store 97.5 in x)  Doesn’t mean we state x is equal to 97.5

Changing Values Assignment  =  Equal sign, but doesn’t mean equal as in math x = 97.5;  Means assign 97.5 to x (or store 97.5 in x)  Doesn’t mean we state x is equal to 97.5 x = x;  Why is this impossible in math?  What does this mean in Java?

Changing boolean and char variables boolean sunny; sunny = false; char letterGrade; letterGrade = ’A’;

Initializing Variables Combining  Declaring a variable (allocating space) and  Assigning an initial value int numberOfStudents = 15; double gpa = 3.14; char letterGrade = ’A’; boolean sunny = true;

Manipulating Data Operators  Arithmetic  Relational  Logical

Arithmetic Operators + - * / %  modulo/reminder  5 % 2 is 1 ++x, x++  Increment x (int) Yields a number

Arithmetic: Division with Integers Math: 5 / 2 is 2.5 Java  “integer division”—both values/operands are integers  5 / 2 has an integer value -- floor of 5/2  5 / 2 is 2 [sometimes this is useful]  If we want a floating point value (2.5)  5 / 2.0, 5.0 / 2, or …  Be careful  int x = 5 / 2.0 ;  x has 2 because 2.5 can’t fit into an int variable

Relational Operators < <= > >= == != Yields true or false value  5 < 2  yields false  not stating 5 is less than 2 (in math), which is impossible  x == 2  Means what?

Logical Operators &&  and ||  or !  not Yields true or false value  true && false is false  !(5 > 2) is false

Precedence/Ordering of Operators x < y + z  (x < y) + z  x < (y + z)

Precedence/Ordering of Operators x < y + z  (x < y) + z  x < (y + z) x < y + z && y < z  x < (y + z) && y < z  ((x < (y + z)) && y) < z  (x < (y + z)) && (y < z)

Precedence/Ordering of Operators Quite natural  Arithmetic (calculate numbers) before  Relational (compare numbers) before  Logical (combine boolean--true/false values) If not sure, add parentheses

Comments Ignore by the compiler  Improves readability, fewer mistakes // describe something that is not obvious /* this is a multi-line comment */

Math Constants and Functions Math.PI, Math.E Math.abs(x) Math.sqrt(x), Math.pow(x, exp) Math.log(x), Math.log10(x) Math.sin(x), Math.cos(x), Math.tan(x) // radians Math.asin(x), Math.acos(x), Math.atan(x) Math.random() // 0 <= num < 1

Input from the Keyboard We’ll usually provide templates for input Scanner keyboard = new Scanner(System.in); x = keyboard.nextInt(); y = keyboard.nextDouble();

Output to the Screen System.out.println( … );  Print the parameter followed by a new line  Examples: System.out.println(15); System.out.println(x); System.out.println(“Hello!”); // “string” System.out.print( … );  Print the parameter without a new line