0% found this document useful (0 votes)
6 views

1 - Introduction To Java (Only When Required)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

1 - Introduction To Java (Only When Required)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

JAVA Introduction

JAVA INTRODUCTION
JAVA BASICS

BASIC DATA TYPES AND VARIABLES

● int - stores integers (whole numbers), without decimals, such as 123 or -123
● float - stores floating point numbers, with decimals, such as 19.99 or -19.99
● char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
● boolean - stores values with two states: true or false
● String - stores text, such as "Hello". String values are surrounded by double quotes

Globant Proprietary | Confidential information


JAVA INTRODUCTION
JAVA BASICS

JAVA OPERATORS
Java divides the operators into the following groups:

● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators

Globant Proprietary | Confidential information


JAVA INTRODUCTION
JAVA BASICS

JAVA OPERATORS
Java divides the operators into the following groups:

● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators

Globant Proprietary | Confidential information


JAVA INTRODUCTION
JAVA BASICS

JAVA OPERATORS
Java divides the operators into the following groups:

● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators

Globant Proprietary | Confidential information


JAVA INTRODUCTION
JAVA BASICS

JAVA OPERATORS
Java divides the operators into the following groups:

● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators

Globant Proprietary | Confidential information


JAVA INTRODUCTION
JAVA BASICS

OPERATORS AND CONDITIONAL STATEMENTS

Java supports the usual logical conditions from mathematics:

● Less than: a < b


● Less than or equal to: a <= b
● Greater than: a > b
● Greater than or equal to: a >= b
● Equal to a == b
● Not Equal to: a != b

Globant Proprietary | Confidential information


JAVA INTRODUCTION
JAVA BASICS

OPERATORS AND CONDITIONAL STATEMENTS

You can use these conditions to perform different actions for different decisions.

● Use if to specify a block of code to be executed, if a specified condition is true


● Use else to specify a block of code to be executed, if the same condition is false
● Use else if to specify a new condition to test, if the first condition is false
● Use switch to specify many alternative blocks of code to be executed

Short Hand If...Else


(Ternary Operator)

Globant Proprietary | Confidential information


JAVA INTRODUCTION
JAVA BASICS

OPERATORS AND CONDITIONAL STATEMENTS

This is how it works:


● The switch expression is evaluated once.
● The value of the expression is compared with the values of each case.

● If there is a match, the associated block of code is executed.


● The break and default keywords are optional (break, breaks out of the
switch block)

Globant Proprietary | Confidential information


JAVA INTRODUCTION
JAVA BASICS

JAVA FOR LOOP

● Statement 1 is executed (one time) before the execution of the code block.

● Statement 2 defines the condition for executing the code block.

● Statement 3 is executed (every time) after the code block has been executed.

Globant Proprietary | Confidential information


JAVA INTRODUCTION
JAVA BASICS

JAVA FOR-EACH LOOP

Is used exclusively to loop through elements in an array

Globant Proprietary | Confidential information


JAVA INTRODUCTION
OOP

OBJECT ORIENTED PROGRAMMING

It is a programming paradigm, that is, a style of program development or a model for solving
computational problems.
It is a highly recommended paradigm, with which you can write ordered code and applications that are
easier to maintain.

• Java is OOP and it’s fast, secure and reliable.


Class

Defines the
structure of

Attributes Methods
Store their features and
Objects Communicate between
state on them with

Globant Proprietary | Confidential information


JAVA INTRODUCTION
OOP

WHAT IS A CLASS?

• Classes are the foundation of object-oriented programming, a class is


a template, template, or model for creating objects.

• A class is made up of characteristics, properties or attributes


(variables) and its behavior (methods) that work on the properties.

Globant Proprietary | Confidential information


JAVA INTRODUCTION
OOP

WHAT IS A METHOD?
A method is a block of code which only runs when it is called.

● You can pass data, known as parameters, into a method.


● Methods are used to perform certain actions, and they are also known as functions.
● Why use methods? To reuse code: define the code once, and use it many times

public void printNumber(int number){


System.out.println("Number " + number);

public int sumNumbers(int num1, int num2){


return num1 + num2;
}

public boolean isEnoughPeople (int numberOfPeople){


return numberOfPeople >= 10;
Globant Proprietary | Confidential information }
JAVA INTRODUCTION
OOP

WHAT IS AN OBJECT?

• Classes are the foundation of object-oriented programming, a class is


a template, template, or model for creating objects.

• A class is made up of characteristics, properties or attributes


(variables) and its behavior (methods) that work on the properties.

Telefono telefono = new Telefono();


telefono.setMarca("Nokia");
telefono.setModelo("1100");
telefono.setColor("Negro");

telefono.llamar("Pablo");
telefono.cortarLlamada();
telefono.informarCaracteristicas();

Globant Proprietary | Confidential information


JAVA INTRODUCTION
OOP

ACCESS MODIFIERS

• They give a higher level of security to applications by restricting access to


attributes, methods and constructors, ensuring that the user must follow a "path" to
access the information.

• Attributes are generally accessed through the get and set methods, since it is
strictly necessary that the attributes of a class be private.

Globant Proprietary | Confidential information


JAVA INTRODUCTION
OOP

WHAT IS ENCAPSULATION?

It makes sure that "sensitive" data is hidden from users. To achieve this, you must:

• Declare class variables/attributes as private


• Provide public get and set methods to access and update the value of a private variable

Globant Proprietary | Confidential information


JAVA INTRODUCTION
OOP

WHAT IS INHERITANCE?
In Java, it is possible to inherit attributes and methods from one class to another. Inheritance concept
could be grouped into two categories:

● subclass (child) - the class that inherits from another class


● superclass (parent) - the class being inherited from

To inherit from a class, use the extends keyword.

Globant Proprietary | Confidential information


JAVA INTRODUCTION
OOP

WHAT IS INHERITANCE?
In Java, it is possible to inherit attributes and methods from one class to another. Inheritance concept
could be grouped into two categories:

● subclass (child) - the class that inherits from another class


● superclass (parent) - the class being inherited from

To inherit from a class, use the extends keyword. It allows code reutilization rather than rewriting

Globant Proprietary | Confidential information


JAVA INTRODUCTION
OOP

WHAT IS INHERITANCE?

Superclass
Subclasses
public class Professional{ public class Engineer extends Professional{

protected String name; private String engineeringType;


protected int experienceYears;
public Engineer(String name, int years, String
type){
public Professional(String name, int years){ this.engineeringType = type;
this.name = name; super(name, years);
this.experienceYears = years; }
} }
} public class Lawyer extends Professional{

public Lawyer(String name, int years){


super(name, years)
}

public void makeLaws(){


//TODO: Implement here
}
}
Globant Proprietary | Confidential information
JAVA INTRODUCTION
OOP

WHAT IS POLYMORPHISM?

In OPP, polymorphism refers to the ability to define


different classes that have identically named
methods or attributes, but behave differently.

Globant Proprietary | Confidential information


JAVA INTRODUCTION
OOP

WHAT IS ABSTRACTION?
It is the process of hiding the implementation details from the
user, providing only the functionality to the user. In other words, the
user accesses the information of what the object does, rather
than how it does it.

● Abstract class: is a restricted class that cannot be used to create


objects (to access it, it must be inherited from another class).

● Abstract method: can only be used in an abstract class, and it


does not have a body. The body is provided by the subclass
(inherited from).

Globant Proprietary | Confidential information


JAVA INTRODUCTION
OOP

WHAT IS INTERFACE?
Another way to achieve abstraction, is with interfaces.

An interface is a completely "abstract class" that is used to group


related methods with empty bodies

To access the interface methods, the interface must be "implemented"


(kinda like inherited) by another class with the implements keyword
(instead of extends).

Globant Proprietary | Confidential information


JAVA INTRODUCTION
ENUMS

JAVA ENUMS

An enum is a special "class" that represents a group of constants (unchangeable variables, like final
variables).

To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a
comma. Note that they should be in uppercase letters:

Globant Proprietary | Confidential information


JAVA INTRODUCTION
ENUMS
//COMPLEX ENUM
public enum Vaso{
JAVA ENUMS JIRAFA(“Jirafa”, 3000),
public class BebidaCerveza{ JARRA(“Jarra”, 1000),
//SIMPLE ENUM VASO(“Vaso”, 570),
enum MarcaCerveza {CORONA, HEINEKEN, BBC} BOTELLA(“Botella”, 350);

private Vaso vaso; private String tipo;


private MarcaCerveza marca; private int cc;

public BebidaCerveza(MarcaCerveza marca, Vaso vaso){ private Vaso(String tipo, int cc){
this.marca = marca; this.tipo = tipo;
this.vaso = vaso; this.cc = cc;
} }

public void servir() { public int getCC() {


System.out.println(“Sirviendo ”+vaso.getCC()+”cc. de ” + marca); return cc;
} }
} public String getTipo() {
return tipo;
}
}

//Probando enumerados
public class TestEnum{

public static void main (String[] args) {


BebidaCerveza cerveza = new BebidaCerveza(BebidaCerveza.MarcaCerveza.CORONA, Vaso.JARRA)
cerveza.servir();
}
}
Globant Proprietary | Confidential information
THANKS FOR
YOUR
ATTENTION!

You might also like