The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991,...

41
The Java Language

Transcript of The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991,...

Page 1: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

The Java Language

Page 2: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Introduction to Java• JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation.

• It was developed by James Gosling and Patrick Naughton.

• Java is the most popular programming language and is commonly used as the programming language for Android applications.

Page 3: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Introduction to Java

• Java is close to C++ in Syntax, the way you write the code but it is more powerful and can be run independent of any operating system.

Page 4: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Phases in Programming Execution

1. Writing of the program is of course done by java programmer like you and me.

2. Compilation of program is done by javac compiler, javac is the primary java compiler included in java development kit (JDK). It takes java program as input and generates java bytecode as output.

3. In third phase, JVM executes the bytecode generated by compiler. This is called program run phase.

Page 5: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick
Page 6: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Writing your First Program

• All Java applications must have a class definition that consists of the keywords public and class followed by the name of the class.

• Curly Braces are used to open and close the entire class

• The opening brace is {

• The closing brace is }

• The start of your program would look like

Page 7: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Writing Your First Program

public class NameofClass

{

}

The name of the class typically begins with an Uppercase letter and describes the class.a java file can have any number of classes but it can have only one public class and the file name should be same as public class name.

Page 8: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Writing Your First Program

• In order to run a class, you must have a main method.

• The main method is where all programs begin. This is where the Java Virtual Machine can run your program.

For now, just know how to write it, you don’t have to understand everything in the method signature yet in order to use it.

Page 9: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Example of a Java Program

public class NameofClass Change NameofClass to a real class name

{

public static void main(String[] args) Required in order to run the class

{

Whatever you want to do in the program is written in the main method

}

}

Page 10: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Creating Output• One of the first things we often learn in programming is how to print

something to the screen – or output.

• In Java we have a Print Stream class that has a print method and a println method that are the ones most commonly used to output

System.out.print(“ what you want to print goes here”);

System.out.println(“ what you want to print”);

The println method will create a new line after printing

Page 11: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Our Online Editor• We will be working with an online editor named Repl.it to create our

programs

• Replit will give you a file named Main as the classname and will not allow you to change it.

public class Main

{public static void main(String[] args){

System.out.print(“Hello World!”);

}}

Page 12: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

First Program

• Use print() and println() to get you name printed as shown:

John DoeJohnDoe

Page 13: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Creating Variables

• A Variable is a storage location in memory for data.

• In Java, you must state what type of data will be stored in the variable by declaring the variable.

• To declare a variable simply means to • state what data type it is

• to give it a name.

• You cannot use a variable if you have not declared it.

Page 14: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Declaring a Variable

• State the type of data to be stored

• There are many data types and we will learn some throughout the course but for now we will focus on two categories of data”• Data that is a numeric value

• Data that is a text value

• Numeric data can be a• Whole number

• Decimal number

Page 15: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Declare a Variable to Store a Whole Number

• The data type we use most often for whole numbers is int

•int stands for Integer, which is a whole number

•When you have data in your program that will never be a decimal value, declare it as an int

int variableName;

Page 16: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Declare a Variable to Store a Decimal Number

• The data type we use most often for decimal numbers is double

• Decimal numbers are called floating point numbers because there is no fixed number of digits before and after the decimal point; that is, the decimal point can float.

• When you have data in your program that could be a decimal value, you can declare it as a double

double variableName;

Page 17: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Naming a Variable

•After you state the type of the variable, you need to give it a name.

• The name needs to be a descriptive name to describe the type of data being stored in the variable.

• There are a few rules to follow when choosing a name

Page 18: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Naming a Variable

• Variable names cannot contain white spaces, for example: int num one; is invalid because the variable name has space in it.

• Variable name should begin with a letter or special characters such as $ and _ Letters are more commonly used.

• After the first character, the name can include numbers, letters, underscores and other characters.

• The variable name should begin with a lowercase letter.

• Variable names are case sensitive in Java. numOne is different than numone

Page 19: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Naming a Variable

• For lengthy variable names that have more than one word you can use camel case which is when you capitlize the start of the second word:

int smallNumber;

int bigNumber;

double taxRate;

Page 20: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Initializing a Variable

• Before you can use a variable, it must be declared.

• Before you use a variable in a calculation or test it or output it, it must also have a value.

• When you give the variable a value at the same time it is declared, it is called initializing it.

• To initialize a variable, simply assign a value to it.

int num1 = 100;

double taxRate = .07;

Page 21: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Values Cannot Include Symbols

• If you want to initialize a variable with a value such as a dollar amount or a percent, you cannot include any symbol as part of the value.

• Example:

I need to store the hourly rate of $12.50 that an employee earns:

double hourlyRate = 12.50; Do not include $

Page 22: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Values Cannot Include Symbols

• I need to store the sales tax rate of 7%. You cannot just remove the % symbol here. You must change the percent to a decimal by moving the decimal point 2 places to the left.

• 7% is equal to .07 as a decimal. This is the value you must use.

double salesTxRate = .07;

Page 23: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Values Cannot Include Symbols

• I need to store the value of the number of students enrolled in a school. This would be a whole number, because you would never have a fraction of a person. This value is 3,950.

• The comma cannot be included in the value.

int numEnrolled = 3950;

Page 24: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Declaring a Variable to Store Text Data• The other main type of data is data that is text, such as a name or

answering a question with Yes or No.

• Text data is known as a String. The String data type is actually class so when you declare a String, you must use a capital letter S.

String name; // declare a String variable named name.

If you do not initialize the String then it will be empty. You may be asking the user to enter their name so then you wouldn't want to initialize it to a value. String values must have quotes around it.

String name = "Mrs. McRae";

Page 25: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Variables can change value in a program

• The reason they are called variables is because they can change value in a program.

• We use the assignment operator to change the value of a variable that has already been declared.

• The assignment operator is simply the = sign. We do not want to think of it as an equal sign because the left side does not equal the right side as it would in math.

• In programming the value on the right gets assigned to the variable on the left of the operator. YOU CANNOT REVERSE THIS

Page 26: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Examplesdouble salary = 45000; // remember, no commas

int yearInSchool = 10; String schoolName = "East Aurora High";

Change the value of the variable – just assign a new value.

salary = 50000; // employee received a raise

yearInSchool = 11;

schoolName = "Aurora University";

Page 27: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Outputting a Variable

• When outputting a variable, just include the name of the variable inside the parenthesis following the print or println method

System.out.println(userName);

• You can also combine text with the value of a variable in the print or println method.

System.out.println("Welcome to my program " +

userName);

Page 28: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Using Comments in a Program• Comments are used to add information that is NOT EXECUTED but

completely ignored by the compiler

• The comments can be used to provide information or explanation about the variable, method, class or any statement.

• They can also be used to hide program code for specific time.

• Single Line Comment:• Use // to ignore everything on the line after the slashes

• Multi- Line Comment• Use /* to start the comment and */ to end the comment, everything in

between will be ignored

Page 29: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Using Comments in a Program

• Examples:

Comments will show up in a different color so you will know it is a comment

Page 30: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Mathematical Operators

• Performing calculations is an important part of working with data.

• You can do your typical mathematical operations necessary to perform calculations required for the program

• Standard Mathmatical Operators: + to add

- to subtract

* to multiply

/ to divide

Page 31: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Mathematical Operators

• You can calculate with literal numbers:salesTax = 56 * .07;

sum = 5 + 6

But it is not very helpful to use literal numbers in a calculation because then it will always be the same calculation.

• Variables are usually used in a calculation to allow the program to be used over and over with different values.

Page 32: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Mathematical Operators• Always Remember:

variable = value;

Example:

double num = 5;

int num2 = 7;

double sum = num + num2;

double difference = num2 - num;

double product = num * num2;

double quotient = num / num2;

Page 33: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

The Result of an Operation

• Be aware of the type of the result of an arithmetic operator.

• If you have 2 values being calculated that are both integers, you will get an integer as a result

• Division is where this the most important to remember because when you divide 2 integers, you NEVER get a fractional answer.

• Two integers can only give you an integer as an answer

Page 34: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Calculating with Integers

• Examples:

int num = 5 ;

int num2 = 3;

int answer = num + num2; // the result is 8

answer = num / num2; // the result is 1

Page 35: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Calculating with at least one double

• If at least one of the values is a double, you will get a double as the result of your calculation.

• Examples:

double num = 5.6 ;

int num2 = 3;

double answer = num + num2; // the result is 8.6

answer = 5.6 / 3; // the result is 1.86

Page 36: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Special Operators• The Modulus Operator

• The Modulus operator is the % symbol on the keyboard but it has nothing to do with percent.

• The modulus operator returns the remainder of a division operation and not the quotient

Example:

int num = 5;

int num2 = 3;

int remainder = num % num2; // result is 2

Page 37: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Special Operators

• Increment Operator• It is a common thing to add 1 to a variable, like in cases when you are adding

1 to a score or a counter.

• The increment operator is a quick way to add 1 to a variable

• The operator is ++ add it to a variable name to add 1 to that variable

int num = 8;

num++; // add 1 to num - num is now 9

num++ is the same as num = num + 1;

Page 38: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Special Operators

• The Decrement Operator• When you need to subtracting 1 from a variable, you can use the decrement

operator to simply subtract 1 from the current value of the variable.

• The operator is --

• Add it to the name of a variable to subtract 1 from that variable

int num = 8;

num--; // subtract 1 from num - num is now 7

num-- is the same as num = num - 1;

Page 39: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Compound Assignment Operators• Sometimes we need to modify the same variable value and reassign it

back to the same variable.

• Java allows you to combine assignment and addition operators using a shorthand operator.

• Examples: Long way to write the expression Using the Compound Operator

num = num + 8; num += 8;

sum = sum - num; sum -= num;

count = count * 2; count *= 2;

num = num / 5; num /= 5;

remainder = remainder % 22 remainder %= 2;

Page 40: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

Getting User Input• The Scanner class is used to get user input

• To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation.

nextDouble()Reads a double value

from the user

nextInt()Reads a int value from

the user

nextLine()Reads a String value

from the user

Page 41: The Java Language - QuiaIntroduction to Java •JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. •It was developed by James Gosling and Patrick

In order to use the Scanner class in your program, you have to import the package at the top of the program:

import java.util.Scanner;