1/104
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Java
is a widely used object-oriented programming language and software platform that runs on billions of devices, including notebook computers, mobile devices, gaming consoles, medical devices and many others.
java
The rules and syntax of _____ are based on the C and C++ languages
Primary goal of java
"write once, run anywhere.
java
was initially developed by James Gosling at Sun Microsystems in the early 1990s. Gosling named the project "Oak" after an oak tree that stood outside his office window.
Variables
refer to Containers for storing data (changeable values)
Constants
refer to fixed values using final.
underscore _ or dollar sign $ characters,
Variable names should not start with ________ even though both are allowed
mnemonic- that is, designed to indicate to the casual observer the intent of its use.
The choice of a variable name should be
One-character variable names
should be avoided except for temporary "throwaway" variables.
i, j, k, m, and n
Common names for temporary variable for integers
c, d, and e
Common names for temporary variables for characters
Data type
a classification that dictates what kind of values a variable can hold in programming, and how the computer should interpret those values.
Primitive
specifies the type of a variable and the kind of values it can hold
Categories of Data Types:
Primitive - specifies the type of a variable and the kind of values it can hold
Non-primitive - such as String, Arrays and Classes
Non-primitive
- such as String, Arrays and Classes
byte
Stores whole numbers from -128 to 127
short
Stores whole numbers from -32,768 to 32,767
int
Stores whole numbers from -2,147,483,648 to 2,147,483,647
long
Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float
Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double
Stores fractional numbers. Sufficient for storing 15 to 16 decimal digits
boolean
Stores true or false values
char
Stores a single character/letter or ASCII values
Open CMD Locate your java code Compile your code (javac file name) Run your code (java class name)
How to run your java program?
VARIABLES
A variables can be considered as a name given to the location in memory where values are stored.
Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_', and character '$'. A name can’t contain space character. Do not start with a digit. A name can be of any length. Upper and lower case count as different characters. So SUM and Sum are different names. A name can not be a reserved word. A name must not already be in use in this part of the program.
VARIABLE NAMES AND KEYWORDS DOS AND DONT
reserved word
is a word which has a predefined meaning in Java. For example int, double, true, and import are ______.
expression
n is a combination of constants (like 100), operators ( like +), variables(section of memory) and parentheses ( like “(” and “)” ) used to calculate a value.
arithmetic operator
r is a symbol that asks for doing some arithmetic.
CONCATENATION
Refers to the operation of joining two strings together. You can join strings using either the addition (+) operator or the String's concat() method
PARENTHESES
Difference between -1 ( 9 - 2 ) 3 - 2 3 and -1 9
To say exactly what numbers go with each operator, use _______
Nested Parentheses
The expression is evaluated starting at the most deeply nested set of parentheses (the "innermost set"), and then working outward until there are no parentheses left.
If there are several sets of parentheses at the same level, they are evaluated left to right.
SCANNER
■ The Scanner class of the java.util package is used to read input data from different sources like input streams, files, etc.
■ The Java Scanner class is widely used to parse text for strings and primitive types using a regular expression.
■ It is the simplest way to get input in Java.
SCANNER
■ It is the simplest way to get input in Java.
Output Import Scanner Class
■ Import the java.util.Scanner package before we can use the Scanner class.
The Scanner class provides various methods that allow us to read inputs of different type
Java Scanner nextInt() Java Scanner nextDouble() Java Scanner next() Java Scanner nextLine()
next()
Reads the next single word (stops at space).
nextLine()
reads the entire line of text (including spaces until Enter is pressed).
nextDouble()
Reads the next input as a decimal number (floating-point).
nextInt()
Reads the next input as an integer (whole number).
Strings
are a fundamental data type that represent sequences of characters.
The String class
is used to create and manipulate strings.
immutable
Strings in Java are ______ meaning once a string object is created, it cannot be modified.
new string object
Any operation that appears to modify a string actually creates a what
double quotes
String literals in Java are enclosed in .
JVM (Java Virtual Machine)
When a string literal is used, the ____checks the string pool to see if the string already exists.
string literal is used → JVM checks the string pool
If it does, the existing string is reused; otherwise, a new string is added to the pool. This optimization reduces memory overhead.
object (java)
An ______t is any entity that has a state and behavior. For example, a bicycle is an object.
States: idle, first gear, etc
Behaviors: braking, accelerating, etc.
Class (java)
is a blueprint for the object. Before we create an object, we first need to define the ______
Class (java)
We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.
length()
: Returns the length of the string. String str = "Hello"; int length = str.length();
charAt(int index)
Returns the character at the specified index. char ch = str.charAt(1);
substring(int beginIndex, int endIndex)
Returns a new string that is a substring of the original string. String sub = str.substring(1, 4);
concat(String str)
Concatenates the specified string to the end of the current string. String greeting = "Hello".concat(", World!");
indexOf(String str)
Returns the index within the string of the first occurrence of the specified substring. int index = str.indexOf("e");
toUpperCase() and toLowerCase()
Convert the string to uppercase or lowercase.
String upper = str.toUpperCase(); String lower = str.toLowerCase();
trim()
: Removes whitespace from both ends of the string. String spaced = " Hello "; String trimmed = spaced.trim();
Control flow statements
in programming control the order in which the computer executes statements in a file. They allow us to regulate the flow of our program's execution.
• Decision-Making Statement
• Loop Statements
• Jump Statements
TYPES OF CONTROL FLOW STATEMENTS IN JAVA
DECISION-MAKING STATEMENT
Assist in making decisions. It decides which statement to execute and when. Java provides the facility to make decisions using selection statements or selection constructs
• if Statement • switch Statement
Java provides two types of decision-making statements:
Selection
is a decision-making process. The outcome of a decision determines which process the system takes.
"If" Statement
is the simplest decision-making statement.
"If" Statement
It is used to decide whether a block of code should be executed or not based on the test condition.
"If" Statement
f the condition is evaluated to true, then that block is executed; otherwise, it is skipped.
"If-Else"
is an extension of the If-statement.
if statement
only tells what to do if the condition evaluates to true.
It does not tell anything when the condition is false
If-Else
provides the provision of an else block, which is executed when the condition is false. It has a provision to accommodate one more block of code.
"If-Else-If" LADDER
consists of an if statement followed by multiple else-if statements. It is used to evaluate a condition using multiple statements. The chain of if statements are executed from the top-down. 30;
NESTED-IF
contains an if statement inside another if statement. Java allows us to nest if equations with another if or an else if statement.
SWITCH STATEMENT
in java is used to execute a single statement from multiple conditions. The switch statement can be used with short, byte, int, long, enum types, etc.
Case values that are duplicate are not permissible
SWITCH STATEMENT
• Usage of break statement is made to terminate the statement sequence. It is optional to use this statement. If this statement is not specified, the next case is executed.
LOOPING STATEMENTS
Statements that execute a block of code repeatedly until a specified condition is met are known as looping statements.
• 1. While • 2. Do-While • 3. For • 4. For-Each
Java provides the user with three types of loops:
WHILE
• Known as the most common loop, the while loop evaluates a certain condition. If the condition is true, the code is executed. This process is continued until the specified condition turns out to be false. • The condition to be specified in the while loop must be a Boolean expression. An error will be generated if the type used is int or a string.
DO-WHILE
• The do-while loop is similar to the while loop, the only difference being that the condition in the do-while loop is evaluated after the execution of the loop body. This guarantees that the loop is executed at least once.
FOR LOOP
• The for loop in java is used to iterate and evaluate a code multiple times. When the number of iterations is known by the user, it is recommended to use the for loop
FOR-EACH
• The traversal of elements in an array can be done by the for-each loop. The elements present in the array are returned one by one. It must be noted that the user does not have to increment the value in the for-each loop.
BREAK
The ______ statement in java is used to terminate a loop and break the current flow of the program
CONTINUE
• To jump to the next iteration of the loop, we make use of the _______ statement.
CONTINUE
This statement continues the current flow of the program and skips a part of the code at the specified condition
Pseudocode
s an artificial and informal language that helps programmers develop algorithms.
is very similar to everyday English.
Flowchart (Dictionary)
A schematic representation of a sequence of operations, as in a manufacturing process or computer program.
Flowchart (Technical)
A graphical representation of the sequence of operations in an information system or program.
Information system
show how data flows from source documents through the computer to final distribution to users.
Program flowcharts
show the sequence of instructions in a single program or subroutine. Different symbols are used to draw each type of flowchart.
A Flowchart
• shows logic of an algorithm
emphasizes individual steps and their interconnections
• e.g. control flow from one action to the next
Oval
Denotes the beginning or end of the program
Parallelogram
Denotes an input operation
Rectangle
Denotes a process to be carried out e.g. addition, subtraction, division etc.
Diamond
Denotes a decision (or branch) to be made. The program should continue along one of two routes. (e.g. IF/THEN/ELSE
Hybrid
Denotes an output operation
Flow line
Denotes the direction of logic flow in the program
Flowcharts
s is a graph used to depict or show a step by step solution using symbols which represent a task.
Flowcharts
The symbols used consist of geometrical shapes that are connected by flow lines
Flowcharts
It is an alternative to pseudocoding; whereas a pseudocode description is verbal, a flowchart is graphical in nature.
Terminal symbo
indicates the beginning and end points of an algorithm.
Process symbol
shows an instruction other than input, output or selection.
Input-output symbol
- shows an input or an output operation.
Disk storage I/O symbol
indicates input from or output to disk storage.