Curso Java SCJP

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

SCJP

1.Java Source And Data Type


1.1.Java Source( 4 ) 1.2.Comments( 3 ) 1.3.package( 5 ) 1.4.import( 6 ) 1.5.static import( 3 ) 1.6.identifier( 14 ) 1.7.Literal( 3 ) 1.8.Primitive Data Types( 5 ) 1.9.Integer( 13 ) 1.10.char( 7 ) 1.11.float( 8 ) 1.12.boolean( 3 ) 1.13.long( 2 ) 1.18.Array Dimension( 7 ) 1.19.main method( 7 ) 1.20.Member Variable( 10 ) 1.21.Method Variable( 8 ) 1.22.static Variable( 1 ) 1.23.Method( 12 ) 1.24.static Method( 1 ) 1.25.Parameter( 10 ) 1.26.variable scope( 6 ) 1.27.Garbage Collection( 8 ) 1.28.Questions( 34 ) 1.29.Questions( 50 ) 1.30.Questions( 32 )

1.14.double( 6 ) 1.15.String( 3 ) 1.16.Array( 13 ) 1.17.Array Elements( 5 )

1.31.Questions( 22 ) 1.32.Questions( 34 ) 1.33.Questions( 36 )

2.Operators
2.1.Operators( 1 ) 2.2.Unary Operators( 8 ) 2.3.Bitwise Operators( 9 ) 2.4.Cast Operators( 3 ) 2.5.Arithmatic Operators( 20 ) 2.6.Comparison Operators( 16 ) 2.7.instanceof Operator( 16 ) 2.8.boolean Operators( 6 ) 2.9.Conditional Operator( 5 ) 2.10.Assignment Operators( 8 ) 2.11.Questions( 18 ) 2.12.Questions( 20 ) 2.13.Questions( 20 ) 2.14.Questions( 38 ) 2.15.Questions( 26 )

3.Modifiers
3.1.Access Modifiers( 6 ) 3.10.super( 3 )

3.2.private( 3 ) 3.3.default modifier( 4 ) 3.4.public( 2 ) 3.5.protected( 3 ) 3.6.final( 10 ) 3.7.abstract( 10 ) 3.8.static( 13 ) 3.9.this( 4 )

3.11.native( 1 ) 3.12.transient( 1 ) 3.13.volatile( 1 ) 3.14.strictfp( 1 ) 3.15.Questions( 18 ) 3.16.Questions( 14 ) 3.17.Questions( 28 )

4.Type Casting
4.1.Type Casting( 4 ) 4.2.Primitive Type Casting( 30 ) 4.3.Reference Type Casting( 16 ) 4.4.Array Type Casting( 9 ) 4.5.Questions( 12 ) 4.6.Questions( 26 )

5.Statements
5.1.while( 3 ) 5.2.do while( 1 ) 5.10.finally( 1 ) 5.11.Exception( 5 )

5.3.for( 16 ) 5.4.enhanced for loop( 3 ) 5.5.break( 2 ) 5.6.continue( 5 ) 5.7.if( 4 ) 5.8.switch( 13 ) 5.9.try catch( 5 )

5.12.throw( 2 ) 5.13.assert( 5 ) 5.14.block( 1 ) 5.15.Questions( 40 ) 5.16.Questions( 46 ) 5.17.Questions( 44 )

6.Object Oriented
6.1.Encapsulation( 1 ) 6.2.Composition( 2 ) 6.3.Inheritance( 2 ) 6.4.overridden method( 19 ) 6.5.overloading method( 14 ) 6.6.Variable Length Argument( 2 ) 6.7.Constructor( 23 ) 6.10.JavaBeans( 3 ) 6.11.enum( 14 ) 6.12.interface( 2 ) 6.13.Questions( 64 ) 6.14.Questions( 60 ) 6.15.Questions( 42 ) 6.16.Questions( 48 )

6.8.Inner Classes( 33 ) 6.9.Anonymous Class( 7 )

6.17.Questions( 42 )

7.Thread
7.1.Thread Creation( 7 ) 7.2.Thread Starting( 5 ) 7.3.Thread Priority( 4 ) 7.4.Daemon Threads( 1 ) 7.5.Thread Join( 1 ) 7.6.Yielding( 1 ) 7.7.Sleeping( 2 ) 7.8.Wait Notify( 4 ) 7.9.Synchronize( 6 ) 7.10.Questions( 32 ) 7.11.Questions( 30 ) 7.12.Questions( 22 ) 7.13.Questions( 26 ) 7.14.Questions( 22 )

8.Utility Classes
8.1.Object( 2 ) 8.2.equals method( 3 ) 8.3.toString method( 2 ) 8.4.hashCode( 1 ) 8.18.SortedSet( 5 ) 8.19.Comparable( 1 ) 8.20.Map( 18 ) 8.21.Arrays( 4 )

8.5.clone method( 1 ) 8.6.Math class( 26 ) 8.7.String class( 27 ) 8.8.StringBuffer( 12 ) 8.9.Wrapper classes( 30 ) 8.10.Boxing Unboxing( 18 ) 8.11.Collection( 13 ) 8.12.Collections( 4 ) 8.13.List( 8 ) 8.14.Vector( 1 ) 8.15.LinkedList( 1 ) 8.16.Set( 4 ) 8.17.TreeSet( 4 )

8.22.generics( 13 ) 8.23.Scanner( 3 ) 8.24.Regex( 11 ) 8.25.Formatter( 19 ) 8.26.Date( 7 ) 8.27.Iterator( 2 ) 8.28.Runtime( 1 ) 8.29.System( 3 ) 8.30.Questions( 36 ) 8.31.Questions( 38 ) 8.32.Questions( 40 ) 8.33.Questions( 38 ) 8.34.Questions( 38 )

9.File
9.1.File( 17 ) 9.5.Serialization( 7 )

9.2.RandomAccessFile( 5 ) 9.3.Stream( 8 ) 9.4.Reader( 2 )

9.6.Questions( 50 ) 9.7.Questions( 14 ) 9.8.Questions( 56 )

1.Java Source And Data Type Java Source 1.1.Java Source


1.1.1.Source Files
All Java source files must end with the .java extension. A source file should contain one top-level public class. If a public class is present, the class name must match the unextended filename. A source file may contain an unlimited number of non-public classes. A file can have only one package statement, but multiple imports. Traduccin:

Todos los archivos fuente de Java deben terminar con la extensin de java. Un archivo de origen debe contener una clase pblica de nivel superior. Si una clase pblica est presente, el nombre de la clase debe coincidir con el nombre de archivo sin extensin. Un archivo de origen puede contener un nmero ilimitado de clases no pblicas. Un archivo puede tener slo una sentencia package, pero mltiples importaciones.

1.1.2. Three top-level compilation units may appear in a file. Tres unidades de compilacin de nivel superior pueden aparecer en un archivo
1.Package declaration 2.Import statements 3.Class, interface, and enum definitions Traduccin: 1.Package declaracin 2. declaraciones de Import 3. Class, interfaz y enumeracin definiciones Ejemplo: package com.java2s.com; import java.util.List; public class MainClass{ private int iValue; }

1.1.3. Three top-level compilation units are not all required. Tres unidades de compilacin de nivel superior no son todos los necesarios.
import java.util.List; public class MainClass{ private int iValue; }

1.1.4. If three top-level compilation units are present, then they must appear in the following order

Si tres unidades de compilacin de primer nivel estn presentes, entonces deben aparecer en el siguiente orden
1.Package declaration 2.Import statements 3.Class, interface, and enum definitions If no a package statement, import statement(s) must be the first lin e(s) in the source code file. If no package or import statements, the class declaration must be the first in the source code file. Import and package statements apply to all classes within a source code file. There's no way to declare multiple classes in a file and have them in different packages, or use different imports. Traduccin: Si no hay un comunicado conjunto, la declaracin de importacin (s) debe ser la primera lnea (s) en el archivo de cdigo fuente. Si no hay paquetes o importacin declaraciones, la declaracin de la clase debe ser el primero en el archivo de cdigo fuente. Declaraciones de importacin y el paquete se aplican a todas las clases en un archivo de cdigo fuente. No hay manera de declarar varias clases en un archivo y tenerlos en diferentes paquetes, o utilizar diferentes importaciones. package com; import java.util.List; public class MainClass{ private int iValue; }

1.2.Comments 1.2.1. Java provides three styles of comments


Java provee tres estilos de comentarios.

/* This is a multiline comment. */ // This is a single-line comment. /** This is a multiline javadoc comment */ The first comment style supports traditional C-language comments. The second comment style supports single line C++ comments. The third comment style is used by the javadoc documentation genera tion tool.
Traduccin: El primer estilo de comentario soportado tradicionalmente en el languaje C comentarios. El segundo estilo de comentario sportado en una lnea C++ Comentario. El tercer estilo de comentario es usado por el javadoc documentation generation tool.

/* File header */ public class MainClass{ /** This is a method */ public static void main(String[] argv){ // output System.out.println();

} } 1.2.2. Comments cannot be nested. Los comentarios no pueden anidarse. If comments appear within a String or character literal, they are treated as part of the String or literal.
Traduccin: Si el comentario aparece con un String o carcter literal, ellos son tratados como parte del String o literal.

public class MainClass{ public static void main(String[] argv){ System.out.println("/* comments */"); } } 1.2.3. Comments can appear at the beginning or end of any line in the source code file Los comentarios pueden aparecer al inicio o al fin de cualquier linea en el codigo fuente del fichero. Comments are independent of any of the positioning rules.
Traduccin: Comentarios son independientes de cualquiera de las reglas de posicionamiento.

public class MainClass{ public static void main(String[] argv){ System.out.println(); }// End of main method }
1.3.package

1.3.1. The package name is a series of elements separated by periods. Los nombres de los paquetes es una serie de elementos separados por periodos. The package statement must be the first noncomment line in a source file.
Traduccin: Las declaraciones de paquetes debe de ser en la primera lnea no comentario en un archivo origen.

package com.yourcompanyname.application; public class MainClass{ private int iValue; } 1.3.2.Packages are identified by the package statement. Los paquetes son identificados por la sentencia de declaracin del paquete package packageName; public class MainClass{

public static void main(String[] argv){ System.out.println(); } } 1.3.3. Use only alphanumeric characters in package names. Usar solo caracteres alfanumricos en nombres de paquetes. package com.yourcompanyname.application; //package com//yourcompanyname\\application; // error public class MainClass{ private int iValue; }

1.3.4. java.lang package is imported by default and does not need to be imported by an import statement. El paquete java.lang is importado por default y este no necesita ser importado por un sentencia de import. public class MainClass{ public static void main(String[] argv){ System.out.println(java.lang.Integer.toString(1)); System.out.println(Integer.toString(2)); } }

1.3.5.Other than java.lang, every other package you use must be named in an import statement Otro que java.lang, cada otro paquete que usa debe ser nombrado in cada sentencia import. Or you must use the fully qualified name of the class every ti me it is mentioned.
Traduccin: O debe utilizar el nombre completo de la clase cada vez que se menciona.

import java.util.Vector; public class MainClass { public static void main(String[] argv) { System.out.println(new Vector()); System.out.println(new java.util.ArrayList()); } } 1.4.import
1.4.1. You may import either an individual class from a package or the entire package.

To import an individual class

import java.util.List; public class MainClass{ private int iValue; }

To import an entire package, simply add an asterisk (*) to the end of the package name. import java.util.*; public class MainClass{ private int iValue; }
1.4.2.White space and comments may appear before or after any of these elements.

// Package declaration package exam.prepguide; // Imports import java.awt.Button; // imports a specific class import java.util.*; // imports an entire package // Class definition public class Test { // class body }

1.4.3.To use classes with the same name in two different packages

import java.util.*; import java.sql.*; public class MainClass{ //Date date = new Date(); // error java.util.Date date = new java.util.Date(); }
1.4.4.Reference class without importing

public class MainClass{ java.util.Vector vec = new java.util.Vector(); }


1.4.5.Reference class with importing

import java.util.Vector; public class MainClass{ Vector vec = new Vector(); }


1.4.6.Java compiler automatically imports all the classes in the java.lang package into every source file.

public class MainClass { public static void main(String[] argv) { java.lang.String s = new String(); System.out.println(s); } }

1.5.static import
1.5.1. Java's static import facility allows you to import static data and methods.

import static java.awt.Color.GREEN; import java.awt.Color; public class MainClass{ Color myColor = GREEN; }
1.5.2.Use the * notation to import all accessible constants from a class.

import java.awt.Color; import static java.awt.Color.*; public class MainClass{ Color myColor = GREEN; }
1.5.3.Static importing gives you access to static methods

import java.awt.Color; import static java.awt.Color.GREEN; import static java.awt.Color.getColor; public class MainClass{ Color myColor = GREEN; public static void main(String[] a){ getColor(null); } }

SCJP Operators Operators

2.1.Operators 2.1.1. Precedence of Numeric Operators


Precedencia de Operadores numricos
Precedence Operator Description

1 1 1 1 2 2 2 3 3 5 5 5 5 6 6 13

++ -+ * / % + < > <= >= == != op=

Increment by 1 (or 1.0) Decrement by 1 (or 1.0) Unary plus Unary minus Multiplication Division Modulo Addition Subtraction Less than Greater than Less than/equal Greater than/equal Equal (identical values) Not equal to op assignment (+=, -=, *=, and so on)

2.2.Unary Operators 2.2.1. Unary operators take only a single operand.


Los operadores unarios tienen un solo operando.

Java provides seven unary operators: The The The The The increment and decrement operators: ++ and -unary plus and minus operators: + and bitwise inversion operator: ~ boolean complement operator: ! cast: ()

Java proporciona siete operadores unarios: Los operadores de incremento y decremento: ++ y -Los unarios ms y menos operadores: + y El operador de inversin de bit a bit: ~ El operador de complemento booleano: El cast: () public class MainClass{ public static void main(){ //The ++ and -- operators modify the value of an expression by adding or subtracting 1. double d=0.12345D; d++; System.out.println(d); } }

1.12345

2.2.2.The unary operators are used to increment, decrement, or change the sign of a value.

Los Operadores Unarios son usados para incrementar, decrementar y cambiar el signo del valor
public class MainClass{ public static void main(String[] argv){ int i = 1; i = -i;//The - unary operator changes the sign of a value. System.out.println(i); } }

-1

2.2.3.

Increment and Decrement


Incremento y decremento

public class MainClass { public static void main(String[] argv) { int x = 5; int y = x++; // y gets the value 5, before incrementing x int y2 = ++x; // y2 gets the value 7, after incrementing } } public class MainClass{ public static void main(){ //The ++ and -- operators modify the value of an expression by adding or //subtracting 1. int i=0; i++; System.out.println(i);

--i; System.out.println(i); } }

1 0

2.2.4.

+ and - operators are applied to a value of byte, char, and short types, the value is converted to an int. + y - son operadores aplicados para el valores de byte, carcter y tipos cortos, el valor es convertido a un entero.

public class MainClass{ public static void main(String[] argv){ byte b = 1; byte b1 = (byte)-b; } }

2.2.5.

The ++ operators may appear before the value (prefix) or after the value (postfix).

++x (prefix) x++ (postfix) --x (prefix)

x-- (postfix) Examples of Pre-and Post- Increment and Decrement Operations Initial Value of x 5 5 5 5 Expression y = x++ y = ++x y = x-y = --x y 5 6 5 4 x 6 6 4 4

When using a prefix expression, the value returned is the value that is calculated after the prefix operator is applied. Cuando se usa un prefijo de expresin, el valor devuelto es el valor que se calcula despus se aplica el operador de prefijo. When using a postfix expression, the value returned is the value of the expression before the postfix operator is applied. Cuando se utiliza una expresin Postfix, el valor devuelto es el valor de la expresin anterior se aplica el operador de sufijo. public class MainClass { public static void main(String[] argv) { int x = 10; int y = ++x; // The value assigned to y is 11 System.out.println(x); System.out.println(y);

x = 10; y = x++; // The value assigned to y is 10 System.out.println(x); System.out.println(y); x = 10; y = --x; // The value assigned to y is 9 System.out.println(x); System.out.println(y); x = 10; y = x--; // The value assigned to y is 10 System.out.println(x); System.out.println(y); } }

11 11 11 10 9 9 9 10

2.2.6.

preincrement or pre-decrement, post-increment or post-decrement pre incremento y pre decremento, post incremento or post decremento

public class MainClass{ public static void main(String[] argv){ int i = 0; int y = 0; y = i++; System.out.println(i); System.out.println(y); } } 1 0 public class MainClass{ public static void main(String[] argv){ int i = 0; int y = 0; y = ++i; System.out.println(i); System.out.println(y); } } 1 1 public class MainClass{ public static void main(String[] argv){ int i = 0; int y = 0; y = i--; System.out.println(i); System.out.println(y); } } -1 0

public class MainClass{ public static void main(String[] argv){ int i = 0; int y = 0; y = --i; System.out.println(i); System.out.println(y); } } -1 -1

2.2.7.

mixing the increment and decrement operators with other operators

public class MainClass{ public static void main(String[] argv){ int x = 2; int y = 3; if ((y == x++) | (x < ++y)) { System.out.println("x = " + x + " y = " + y); } } }

x = 3 y = 4

2.2.8.
public class MainClass{ public static void main(String[] argv){ final int x = 5; int y = x++; } }

increment or decrement operators on a final variable.

The final local variable x cannot be assigned. It must be blank and not using a compound assignment

You might also like