• Barajar
    Activar
    Desactivar
  • Alphabetizar
    Activar
    Desactivar
  • Frente Primero
    Activar
    Desactivar
  • Ambos lados
    Activar
    Desactivar
  • Leer
    Activar
    Desactivar
Leyendo...
Frente

Cómo estudiar sus tarjetas

Teclas de Derecha/Izquierda: Navegar entre tarjetas.tecla derechatecla izquierda

Teclas Arriba/Abajo: Colvea la carta entre frente y dorso.tecla abajotecla arriba

Tecla H: Muestra pista (3er lado).tecla h

Tecla N: Lea el texto en voz.tecla n

image

Boton play

image

Boton play

image

Progreso

1/99

Click para voltear

99 Cartas en este set

  • Frente
  • Atrás
  • 3er lado (pista)
(Java Wrapper Classes) What are Java Wrapper Classes?
Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.
(Java Wrapper Classes) Which are the equivalent wrapper class of each primitive type?
(Java Wrapper Classes) When to use wrapper classes?
Sometimes you must use wrapper classes, for example when working with Collection objects, such as ArrayList, where primitive types cannot be used (the list can only store objects).
(Java Wrapper Classes) Example 1
ArrayList<int> myNumbers = new ArrayList<int>(); // Invalid
ArrayList<Integer> myNumbers = new ArrayList<Integer>(); // Valid
(Java Wrapper Classes) Example 2
import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
myNumbers.add(10);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(25);
for (int i : myNumbers) {
System.out.println(i);
}
}
}
10
15
20
25
(Java Wrapper Classes) How to create a wrapper object?
To create a wrapper object, use the wrapper class instead of the primitive type.
To get the value, you can just print the object.
(Java Wrapper Classes) Creating Wrapper Objects: Example
public class Main {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
}
}
5
5.99
A
(Java Wrapper Classes) Since you're now working with objects, you can use certain methods to get information about the specific object.
For example, the following methods are used to get the value associated with the corresponding wrapper object:
intValue(), byteValue(), shortValue(), longValue(), floatValue(), doubleValue(), charValue(), booleanValue()
(Java Wrapper Classes) This example will output the same result as the example above:
public class Main {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt.intValue());
System.out.println(myDouble.doubleValue());
System.out.println(myChar.charValue());
}
}
5
5.99
A
(Java Wrapper Classes) Another useful method is the toString() method, which is used to convert wrapper objects to strings.
In the following example, we convert an Integer to a String, and use the length() method of the String class to output the length of the "string":
(Java Wrapper Classes) Example
public class Main {
public static void main(String[] args) {
Integer myInt = 100;
String myString = myInt.toString();
System.out.println(myString.length());
}
}
3
(Java Exceptions - Try...Catch) Java Exceptions
When executing Java code, different errors can occur:
coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.
(Java Exceptions - Try...Catch) When an error occurs, Java will normally stop and generate an error message.
The technical term for this is:
Java will throw an exception (throw an error).
(Java Exceptions - Try...Catch) What does the try statement allows you?
The try statement allows you to define a block of code to be tested for errors while it is being executed.
(Java Exceptions - Try...Catch) What does the catch statement allows you?
The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
(Java Exceptions - Try...Catch) Syntax. The try and catch keywords come in pairs:
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
(Java Exceptions - Try...Catch) Consider the following example:
This will generate an error, because myNumbers[10] does not exist.
public class Main {
public static void main(String[] args) {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
}
}
(Java Exceptions - Try...Catch) Consider the following example part 2
The output will be something like this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at MyClass.main(MyClass.java:4)
(Java Exceptions - Try...Catch) Example: If an error occurs, we can use try...catch to catch the error and execute some code to handle it:
public class Main {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}
Something went wrong.
(Java Exceptions - Try...Catch) What does the finally statement let you?
The finally statement lets you execute code, after try...catch, regardless of the result.
(Java Exceptions - Try...Catch) What does the finally statement let you? (Example)
public class Main {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
} finally {
System.out.println("The 'try catch' is finished.");
}
}
}
Something went wrong.
The 'try catch' is finished.
(Java Exceptions - Try...Catch) What does the throw statement allow you?
The throw statement allows you to create a custom error.
(Java Exceptions - Try...Catch) The throw statement is used together with an exception type.
There are many exception types available in Java:
ArithmeticException, FileNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, etc:
(Java Exceptions - Try...Catch) Example: Throw an exception if age is below 18 (print "Access denied"). If age is 18 or older, print "Access granted":
public class Main {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
} else {
System.out.println("Access granted - You are old enough!");
}
}

public static void main(String[] args) {
checkAge(15);
}
}
Exception in thread "main" java.lang.ArithmeticException: Access denied - You must be at least 18 years old.
at Main.checkAge(Main.java:4)
at Main.main(Main.java:12)
(Java Exceptions - Try...Catch) Example 2. If age was 20, you would not get an exception:
public class Main {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
} else {
System.out.println("Access granted - You are old enough!");
}
}

public static void main(String[] args) {
checkAge(20);
}
}
Access granted - You are old enough!
(Java Regular Expressions) What is a Regular Expression?
A regular expression is a sequence of characters that forms a search pattern.
(Java Regular Expressions) How can you use this search pattern?
When you search for data in a text, you can use this search pattern to describe what you are searching for.
A regular expression can be a single character, or a more complicated pattern.
(Java Regular Expressions) How can Regular expressions be used?
Regular expressions can be used to perform all types of text search and text replace operations.
(Java Regular Expressions) Java does not have a built-in Regular Expression class, but ...
but we can import the java.util.regex package to work with regular expressions. The package includes the following classes:
- Pattern Class - Defines a pattern (to be used in a search)
- Matcher Class - Used to search for the pattern
- PatternSyntaxException Class - Indicates syntax error in a regular expression pattern
(Java Regular Expressions) Example: Find out if there are any occurrences of the word "w3schools" in a sentence:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("w3schools", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("Visit W3Schools!");
boolean matchFound = matcher.find();
if(matchFound) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
}
Match found
(Java Regular Expressions) Example part 2
In this example, The word "w3schools" is being searched for in a sentence.
First, the pattern is created using the Pattern.compile() method.
(Java Regular Expressions) Example part 3
The first parameter indicates which pattern is being searched for and the second parameter has a flag to indicates that the search should be case-insensitive.
The second parameter is optional.
(Java Regular Expressions) Example part 4
The matcher() method is used to search for the pattern in a string.
It returns a Matcher object which contains information about the search that was performed.
(Java Regular Expressions) Example part 5
The find() method returns true if the pattern was found in the string and false if it was not found.
(Java Regular Expressions) Flags
Flags in the compile() method change how the search is performed.
Here are a few of them:
(Java Regular Expressions) (Flags) Pattern.CASE_INSENSITIVE
The case of letters will be ignored when performing a search.
(Java Regular Expressions) (Flags) Pattern.LITERAL
Special characters in the pattern will not have any special meaning and will be treated as ordinary characters when performing a search.
(Java Regular Expressions) (Flags) Pattern.UNICODE_CASE
Use it together with the CASE_INSENSITIVE flag to also ignore the case of letters outside of the English alphabet
(Java Regular Expressions) What is the first parameter of the Pattern.compile()?
The first parameter of the Pattern.compile() method is the pattern. It describes what is being searched for.
Brackets are used to find a range of characters:
(Java Regular Expressions) (Regular Expression Patterns) This expression find one character from the options between the brackets
Expression: [abc]
Description: Find one character from the options between the brackets
(Java Regular Expressions) (Regular Expression Patterns) This expression find one character NOT between the brackets
Expression: [^abc]
Description: Find one character NOT between the brackets
(Java Regular Expressions) (Regular Expression Patterns) This expression find one character from the range 0 to 9
Expression: [0-9]
Description: Find one character from the range 0 to 9
(Java Regular Expressions) What are Metacharacters?
Metacharacters are characters with a special meaning.
(Java Regular Expressions) (Metacharacters) This metacharacter finds a match for any one of the patterns separated by | as in: cat|dog|fish
Metacharacter: |
Description: Find a match for any one of the patterns separated by | as in: cat|dog|fish
(Java Regular Expressions) (Metacharacters) This metacharacter finds just one instance of any character.
Metacharacter: .
Description: Find just one instance of any character
(Java Regular Expressions) (Metacharacters) This metacharacter finds a match as the beginning of a string as in: ^Hello
Metacharacter: ^
Description: Finds a match as the beginning of a string as in: ^Hello
(Java Regular Expressions) (Metacharacters) This metacharacter finds a match at the end of the string as in: World$
Metacharacter: $
Description: Finds a match at the end of the string as in: World$
(Java Regular Expressions) (Metacharacters) This metacharacter finds a digit
Metacharacter: \d
Description: Find a digit
(Java Regular Expressions) (Metacharacters) This metacharacter finds a whitespace character
Metacharacter: \s
Description: Find a whitespace character
(Java Regular Expressions) (Metacharacters) This metacharacter finds a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b
Metacharacter: \b
Description: Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b
(Java Regular Expressions) (Metacharacters) This metacharacter finds the Unicode character specified by the hexadecimal number xxxx
Metacharacter: \uxxxx
Description: Find the Unicode character specified by the hexadecimal number xxxx
(Java Regular Expressions) What are Quantifiers?
Quantifiers define quantities.
(Java Regular Expressions) (Quantifiers) This quantifier matches any string that contains at least one n
Quantifier: n+
Description: Matches any string that contains at least one n
(Java Regular Expressions) (Quantifiers) This quantifier matches any string that contains zero or more occurrences of n
Quantifier: n*
Description: Matches any string that contains zero or more occurrences of n
(Java Regular Expressions) (Quantifiers) This quantifier matches any string that contains zero or one occurrences of n
Quantifier: n?
Description: Matches any string that contains zero or one occurrences of n
(Java Regular Expressions) (Quantifiers) This quantifier matches any string that contains a sequence of X n's
Quantifier: 
Description: Matches any string that contains a sequence of X n's
(Java Regular Expressions) (Quantifiers) This quantifier matches any string that contains a sequence of X to Y n's
Quantifier: n{x,y}
Description: Matches any string that contains a sequence of X to Y n's
(Java Regular Expressions) (Quantifiers) This quantifier matches any string that contains a sequence of at least X n's
Quantifier: n{x,}
Description: Matches any string that contains a sequence of at least X n's
(Java Threads) What does Threads allow?
Threads allows a program to operate more efficiently by doing multiple things at the same time.
Threads can be used to perform complicated tasks in the background without interrupting the main program.
(Java Threads) There are two ways to create a thread.
It can be created by extending the Thread class and overriding its run() method: Extend syntax.
public class Main extends Thread {
public void run() {
System.out.println("This code is running in a thread");
}
}
(Java Threads) Another way to create a thread is to implement the Runnable interface:
Implement Syntax:
public class Main implements Runnable {
public void run() {
System.out.println("This code is running in a thread");
}
}
(Java Threads) Running Threads.
If the class extends the Thread class, the thread can be run by creating an instance of the class and call its start() method.
(Java Threads) Extend Example
public class Main extends Thread {
public static void main(String[] args) {
Main thread = new Main();
thread.start();
System.out.println("This code is outside of the thread");
}
public void run() {
System.out.println("This code is running in a thread");
}
}
This code is outside of the thread
This code is running in a thread
(Java Threads) If the class implements the Runnable interface, ...
the thread can be run by passing an instance of the class to a Thread object's constructor and then calling the thread's start() method:
(Java Threads) Implement Example
public class Main implements Runnable {
public static void main(String[] args) {
Main obj = new Main();
Thread thread = new Thread(obj);
thread.start();
System.out.println("This code is outside of the thread");
}
public void run() {
System.out.println("This code is running in a thread");
}
}
This code is outside of the thread
This code is running in a thread
(Java Threads) What are the differences between "extending" and "implementing" Threads?
The major difference is that when a class extends the Thread class, you cannot extend any other class,
but by implementing the Runnable interface, it is possible to extend from another class as well, like: class MyClass extends OtherClass implements Runnable.
(Java Threads) What are concurrency problems?
Because threads run at the same time as other parts of the program, there is no way to know in which order the code will run.
When the threads and main program are reading and writing the same variables, the values are unpredictable. The problems that result from this are called concurrency problems.
(Java Threads) (Concurrency Problems)
public class Main extends Thread {
public static int amount = 0;

public static void main(String[] args) {
Main thread = new Main();
thread.start();
System.out.println(amount);
amount++;
System.out.println(amount);
}

public void run() {
amount++;
}
}
0
2
(Java Threads) How to avoid concurrency problems?
To avoid concurrency problems, it is best to share as few attributes between threads as possible.
(Java Threads) If attributes need to be shared, one possible solution is __.
to use the isAlive() method of the thread to check whether the thread has finished running before using any attributes that the thread can change.
(Java Threads) (Example) Use isAlive() to prevent concurrency problems:
public class Main extends Thread {
public static int amount = 0;

public static void main(String[] args) {
Main thread = new Main();
thread.start();
// Wait for the thread to finish
while(thread.isAlive()) {
System.out.println("Waiting...");
}
// Update amount and print its value
System.out.println("Main: " + amount);
amount++;
System.out.println("Main: " + amount);
}
public void run() {
amount++;
}
}
Waiting...
Main: 1
Main: 2
(Java Lambda Expressions) What are Java Lambda Expressions?
A lambda expression is a short block of code which takes in parameters and returns a value.
(Java Lambda Expressions) What is the difference between Lambda and methods?
Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
(Java Lambda Expressions) What is the syntax?
The simplest lambda expression contains a single parameter and an expression:
parameter -> expression
(Java Lambda Expressions) What is the syntax 2?
To use more than one parameter, wrap them in parentheses.
(parameter1, parameter2) -> expression
(Java Lambda Expressions) Expressions are limited.
They have to immediately return a value, and they cannot contain variables, assignments or statements such as if or for.
In order to do more complex operations, a code block can be used with curly braces.
(Java Lambda Expressions) If the lambda expression needs to return a value, then the code block should have a return statement.
(parameter1, parameter2) -> { code block}
(Java Lambda Expressions) Using Lambda Expressions
Lambda expressions are usually passed as parameters to a function:
(Java Lambda Expressions) (Example) Use a lamba expression in the ArrayList's forEach() method to print every item in the list:
import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(9);
numbers.add(8);
numbers.add(1);
numbers.forEach( (n) -> { System.out.println(n); } );
}
}
5
9
8
1
(Java Lambda Expressions) How Lambda expressions can be stored in variables?
Lambda expressions can be stored in variables if the variable's type is an interface which has only one method.
The lambda expression should have the same number of parameters and the same return type as that method
(Java Lambda Expressions) Java has many of these kinds of interfaces built in, such as ...
Java has many of these kinds of interfaces built in, such as the Consumer interface (found in the java.util package) used by lists.
(Java Lambda Expressions) Example: Use Java's Consumer interface to store a lambda expression in a variable
import java.util.ArrayList;
import java.util.function.Consumer;

public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(9);
numbers.add(8);
numbers.add(1);
Consumer<Integer> method = (n) -> { System.out.println(n); };
numbers.forEach( method );
}
}
5
9
8
1
(Java Lambda Expressions) How to use a lambda expression in a method?
To use a lambda expression in a method, the method should have a parameter with a single-method interface as its type.
Calling the interface's method will run the lambda expression.
(Java Lambda Expressions) Example. Create a method which takes a lambda expression as a parameter:
interface StringFunction {
String run(String str);
}

public class Main {
public static void main(String[] args) {
StringFunction exclaim = (s) -> s + "!";
StringFunction ask = (s) -> s + "?";
printFormatted("Hello", exclaim);
printFormatted("Hello", ask);
}
public static void printFormatted(String str, StringFunction format) {
String result = format.run(str);
System.out.println(result);
}
}
Hello!
Hello?
(Java Files) File handling is an important part of any application.
Java has several methods for creating, reading, updating, and deleting files.
(Java Files) Java File Handling
The File class from the java.io package, allows us to work with files.
(Java Files) How to use the File class?
To use the File class, create an object of the class, and specify the filename or directory name.
(Java Files) How to use the File class? (Example)
import java.io.File; // Import the File class

File myObj = new File("filename.txt"); // Specify the filename
(Java Files) The File class has many useful methods for creating and getting information about files.
For example:
(Java Files) Method to test whether the file is readable or not
Method: canRead()
Type: Boolean
Description: Tests whether the file is readable or not
(Java Files) Method to Test whether the file is writable or not
Method: canWrite()
Type: Boolean
Description: tests whether the file is writable or not
(Java Files) Method to create an empty file
Method: createNewFile()
Type: Boolean
Description: Creates an empty file
(Java Files) Method to delete a file
Method: delete()
Type: Boolean
Description: Deletes a file
(Java Files) Method to test whether the file exists
Method: exists()
Type: Boolean
Description: Tests whether the file exists
(Java Files) Method to return the name of the file
Method: getName()
Type: String
Description: Returns the name of the file
(Java Files) Method to return the absolute pathname of the file
Method: getAbsolutePath()
Type: String
Description: Returns the absolute pathname of the file
(Java Files) Method to return the size of the file in bytes
Method: length()
Type: Long
Description: Returns the size of the file in bytes
(Java Files) Method to Return an array of the files in the directory
Method: list()
Type: String[]
Description: Returns an array of the files in the directory
(Java Files) Method to create a directory
Method: mkdir()
Type:  Boolean
Description: Creates a directory