Sie sind auf Seite 1von 8

Code academy - Java

Data Types I: int


int is short for interger, which are all positive and negative numbers, including zero.

The int data type onlu allows values between -2.147.483.648 +2.147.483.647

Data types II : boolean


A boolean is a data that can only be either true or false.

Data types III : char


The char data type is used to represent single characters. That includes the keys on a keyboard
that are used to produce text.

Char is short for character and can represent a single character.

All char values must be enclosed in single quotes, like this : 'G'.

A semicolon ; is also used to end

Java single code statements

Variables
1. A variable stores a value

2. In Java, all variables must have data type.

Comments
A comment is a text that you want Java to ignore. Comments allow you to describe code or keep notes.

There are two types of comments:

1. Single line comments.

// This is a single line comment.

2. Multy-line comments.

begin with /* and end with */

/*
this is

multy-line

comment

*/

Math :
+, -, /, *

% - modulo operator - retruns the ramainder of dividng two numbers.

For example 15%6 the value is 3.

Relational Operators
< : less than.

<= : less than or equal to.

> : greater than.

>= : greater than or equal to.

A relational operator is placed between two operands.

Equality Operators
== : equal to.

!= : not equal to.

Generalization
Data Types are int, boolean, and char.
Variables are used to store values.

Whitespace helps make code easy to read for you and others.

Comments describe code and its purpose.

Arithmetic Operators include +, -, *, /, and %.

Relational Operators include <, <=, >, and >=.

Equality Operators include == and !=.

Boolean Operators: &&

The and operator is represented in Java by &&.

It will returns a boolean valeu of True only if both sides of the expressions are
true.

Boolean Operators : ||
The or operator is represented in Java by ||.

It returns a boolean value of True if at least one side of the expression is true.
Boolean Operators : !
The not operator is represented in Java by !.

It will return the oposite of the expresion immediately after it.

It will return false if the expresion is true , and true if the expresion is false.

Boolean Operators : Precedence

If Statement
In Java the keyword if is the first part of a conditional expression.

It is followed by a boolean expression and the a block of code. If the Boolean expression evalua
tes to true, the block of code that follows will be run.

If-Else Statement
The if/else conditinal will run the block of code associated with the if statement if its Boolean expresion
evaluates to True.

Otherwise, if the expression evaluates to false, it will run the block of code after the else keyword.

If-ElseIf-Else Statement:
If the Boolean expresion after if statement evaluates to true, it will run the code block that directly
follows.

otherwise, if the Boolean expresion after else if evaluates to true, the code block that directly follow will
run.

Finnaly if all previous Boolean expresions are evaluated to false, the code with the else block will run.
Tenary Conditional
The term tenary comes from a Latin word that means "composed of three parts"

These three parts are:

A Boolean expression.

A single statement that gets executed if the Boolean expression is true.

A single statement that gets executed if the Boolean expression is false.


Switch Statement
The conditional statements that we have covered so far require Boolean expressions to determine wich
code block to run. Java also provides a way to execute code blocks based whether a block is equal to a
specific value. For those specific cases, we can use the switch statement, wich helps keep code organized
and less wordy.
The break statement will exit the switch statement after a condition is met. Without the break statement
, Java will continue to run the code to check if the value of that switch will match another cases.

The default case is printed only if restaurant Rating is not equal to an int with the value of 1 2 or 3.

Das könnte Ihnen auch gefallen