Programming Languages Set of instructions that only computers or machines can read or interpret. Example: Java, Python, Ruby on Rails etc
What is Java? Java is one of the most popular programming languages in the world. With Java you can build various types of applications such as desktop, web, mobile apps and distributed systems. Java is also a case-sensitive programming language.
5 Interesting Facts about Java Java was developed by James Gosling in 1995 at Sun Microsystems (later acquired by Oracle). It was initially called Oak. Later it was renamed to Green and was finally renamed to Java inspired by Java coffee. Java has close to 9 million developers worldwide. About 3 billion mobile phones run Java, as well as 125 million TV sets and every Blu-Ray player. According to indeed.com, the average salary of a Java developer is just over $100,000 per year in the US.
Hierarchy of Java Applications Methods are the smallest building blocks in Java. In other programming languages, these methods are called functions . One or more methods are called classes and related classes in packages . This modularity in Java allows us to break down large programs into smaller building blocks that are easier to understand and re-use.
Java Syntax All codes in Java must be ran or runs inside the class Main and should always inside a class. Note: Java is case-sensitive: " MyClass " and " myclass " has different meaning. When saving, filename should be the same with the class name with extension name of “.java”
Java Syntax
Activity #1
Let’s Try to Write Our Very First Program!
CONGRATULATIONS! You have successfully written your very first program using Java Programming Language!
Variables We use variables to temporarily store data in computer’s memory. In Java, the type of a variable should be specified at the time of declaration. In Java, we have two categories of types: Primitives: for storing simple values like numbers, strings and booleans . Reference Types: for storing complex objects like email messages.
Primitive Types
Declaring Variables
Reference Types In Java we have 8 primitive types. All the other types are reference types. These types don’t store the actual objects in memory. They store the reference (or the address of) an object in memory. To use reference types, we need to allocate memory using the new operator. The memory gets automatically released when no longer used.
String String is reference types, but we don’t need to use the new operator to allocate memory to them. We can declare string variables like the primitives since we use them a lot. String name = “ Ferdie ”;
Useful String Methods The String class in Java provides several useful methods: startsWith (“a”) endsWith (“a”) length() indexOf (“a”) replace(“a”, “b”) toUpperCase () toLowerCase ()
Useful String Methods Strings are immutable, which means once we initialize them, their value cannot be changed. All methods that modify a string (like toUpperCase ) return a new string object. The original string remains unaffected.
Comments We use comments to add notes to our code. // This is a comment, and it won’t executed.
Escape Sequences If you need to use a backslash or a double quotation mark in a string, you need to prefix it with a backslash. This is called escaping. Common escape sequences: \\ \” \n (new line) \t (tab)
Activity #2
Let’s Print Our First Variable!
Activity #3
We’re going to initiate other variable types!
JAVA Operators Operator in Java is a symbol that is used to perform operations. example: +, -, *, / etc. There are many types of operators in Java.
JAVA Operators Arithmetic Operator Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations.
JAVA Operators Arithmetic Operator
Activity #4
Do the math! With the same code as the above slide, do the other 3 arithmetic operators: - (subtraction) * (multiplication) / (division)
4 th Quarter
High-Level Programming High-level programming refers to the use of programming languages that are designed to be easy to read, write, and understand by humans. These languages abstract away many low-level details of the computer's architecture, such as memory management and hardware interactions, allowing programmers to focus more on solving problems and implementing algorithms rather than dealing with low-level technicalities.
High-Level Programming Key characteristics of high-level programming languages include: Abstraction: High-level languages provide abstractions that allow programmers to work with concepts closer to the problem domain rather than the machine architecture. This makes it easier to express complex ideas and algorithms in a concise and readable manner. Portability: Programs written in high-level languages are generally portable across different platforms and operating systems, as the language runtime or interpreter abstracts away platform-specific details.
High-Level Programming Productivity: High-level languages often come with built-in libraries and frameworks that provide pre-defined functions and data structures, saving developers time and effort in implementing common tasks. Readability: High-level languages are designed to be easily readable and understandable by humans, with syntax and semantics that resemble natural language as much as possible. Less Prone to Errors: Due to their higher level of abstraction and built-in features, high-level languages are often less prone to errors compared to low-level languages.
High-Level Programming Examples of high-level programming languages include Python, Java, C#, JavaScript, and Ruby. These languages are widely used in various domains, including web development, software engineering, data science, and artificial intelligence.
Compilers What is compilers? A compiler is a software tool or program that translates source code written in a high-level programming language into machine code or executable code that can be directly executed by a computer's processor. The process of translation performed by a compiler is called compilation.
Compilers Translation Process: The primary role of a compiler is to translate high-level programming language code into machine-readable instructions. This translation process enables the execution of programs written in high-level languages on a computer. Abstraction Layer: Compilers serve as an abstraction layer between high-level programming languages and machine code. They hide the complexities of the underlying hardware architecture, allowing developers to write code in a language that is more intuitive and productive. Execution: Once the code is compiled, the resulting executable file can be run on a computer without needing the original source code or the compiler itself. This separation between development and execution is a key aspect of the relationship between high-level programming languages and compilers.
Compilers In summary, compilers play a crucial role in bridging the gap between high-level programming languages and machine code, enabling developers to write code in a more human-readable and expressive manner while still being able to execute it efficiently on a computer.
Relationships of High-Level Programming, compilers and Computers
True or False _____1. 5 == 6 False: 5 is not equal to 6 _____2. 9 >= 10 False: 9 is not greater than or equal to 10 _____3. 7 <= 7 True: 7 is greater than or equal to 7 _____4. 8 != 8 False: 8 is equal to 8 _____5. 17 > 17 False: 17 is not greater than 17
Activity #6
True or False int myName = 40; _____1. myName >= 39? _____2. myName <= 49? int firstNumber = 7; int secondNumber = 8; _____3. firstNumber > secondNumber ? _____4. firstNumber < secondNumber ? _____5. firstNumber != secondNumber ?
True or False int myName = 40; _____1. myName >= 39? True: 40 is greater than 39 _____2. myName <= 49? True: 40 is less than 49 int firstNumber = 7; int secondNumber = 8; _____3. firstNumber > secondNumber ? False: 7 is not greater than 8 _____4. firstNumber < secondNumber ? True: 7 is less than 8 _____5. firstNumber != secondNumber ? True: 7 is not equal to 8
Java Control Statement Java control statements are programming constructs that control the flow of execution in a Java program based on specified conditions. They enable you to make decisions, repeat tasks, and branch execution based on various conditions.
Java Control Statement Here's a breakdown of the main types of control statements in Java: Conditional Statements: if-else: Executes a block of code if a specified condition is true, otherwise executes an alternative block of code.
Java Control Statement Control statements are essential for writing programs that can make decisions, perform repetitive tasks, and handle different scenarios based on specific conditions.
Java Control Statement Syntax of if statement if ( condition ){ block of codes ; } Syntax of if-else statement If ( condition ) { block of codes ; } else { block of codes; }
Java Control Statement
Activity # 7
if-else Statement If their age is above 40, print “too old” if their age is 40 and below, print “too young”
JAVA User Input (Scanner Class) The Scanner class is used to get user input, and it is found in the java.util package. Example: The program will ask your name: System.out.println(“What is your name?”); The user inputs his name and the output will be: What is your name? Ferdinand Molina
JAVA User Input (Scanner Class)
JAVA User Input (Scanner Class)
JAVA User Input (Scanner Class) This code will tell your program that you will be needing a User Input. System.out will output whatever you want your program to output and System.in will let you save values to be printed. This code will tell your program that you will be saving the User Input in the variable myName.
JAVA User Input (Scanner Class) Scanner will not work if you will not import the Scanner Class. All imports are placed before your Main Class.
Activity #8
User Input: Name Write a java program using scanner to ask the user to enter his/her name and print it.
String Concatenation The + operator can be used between strings to combine them. This is called concatenation . Example: String firstName = "John"; String lastName = "Doe"; System.out.println(firstName + " " + lastName);
Activity #9
User Input: Name Write a java program using scanner to ask the user to enter his/her name and print it. Concatenate the line “Your name is: ”
Activity #10
Simple Calculator Write a program that will ask the user 2 numbers and print 4 outputs. Use the four arithmetic operators example: What is your first number? 7 What is your second number? 7 Add: 14 Subtract: 0 Divide: 1 Multiply: 49