Skip to content

Commit

Permalink
feat(lang-enh): 馃幐 String Objects, translated
Browse files Browse the repository at this point in the history
Refers: #4
  • Loading branch information
rcmoutinho committed Sep 10, 2019
1 parent b901036 commit 3d91f17
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,88 +1,86 @@
:java-package: src/org/j6toj8/languageenhancements
:section-java-package: ../../../{java-package}

=== Objetos Strings
=== String objects

.Objetivo
.Objective
----
Develop code that uses String objects in the switch statement, binary literals, and numeric literals, including underscores in literals.
-
Desenvolver c贸digo que utilize objetos String em instru莽玫es Switch, bin谩rios literais, e num茅ricos literais, incluindo underscore (_) em literais.
----

==== String em instru莽玫es Switch
==== String in the switch statement

脡 esperado que o candidato saiba compreender e analisar o uso de Strings em instru莽玫es `switch`, como no seguinte exemplo.
The candidate is expected to understand and analyze the use of Strings in `switch` statements, as in the following example.

[source,java,indent=0]
.{java-package}/stringinswitch/StringInSwitch_Complete.java
----
include::{section-java-package}/stringinswitch/StringInSwitch_Complete.java[tag=code]
----

Apesar da certifica莽茫o ter foco nas atualiza莽玫es trazidas pelo Java 7 e 8, 茅 esperado que o candidato entenda tamb茅m conceitos de vers玫es anteriores do Java. Por isso, ser茫o apresentadas algumas regras que talvez voc锚 j谩 conhe莽a sobre `switch`, mas utilizando `String` no `switch`.
Although certification focuses on updates brought by Java 7 and 8, the candidate is expected to understand concepts from previous versions of Java as well. Therefore, some rules you may already know about `switch` will be presented but using `String` on the `switch`.

. Todo `case` deve ser 煤nico, n茫o pode se repetir.
. Every `case` must be unique, cannot be repeated.

. O `default` pode aparecer em qualquer posi莽茫o no `switch`.
. The `default` can appear anywhere on the `switch`.
+
.{java-package}/stringinswitch/StringInSwitch_Default.java
[source,java,indent=0]
----
include::{section-java-package}/stringinswitch/StringInSwitch_Default.java[tag=code]
----

. Tipos suportados em `switch`.
* int e Integer
* byte e Byte
* short e Short
* char e Character
. Supported types in `switch`.
* int and Integer
* byte and Byte
* short and Short
* char and Character
* String
* valores de Enums
* Enums values

. Tipos n茫o suportados em `switch`.
. Types not supported in `switch`.
+
.{java-package}/stringinswitch/StringInSwitch_Type.java
[source,java,indent=0]
----
include::{section-java-package}/stringinswitch/StringInSwitch_Type.java[tag=code]
----

. A execu莽茫o se inicia em um `case` e somente para ao encontrar um `break`.
. Execution starts in a `case` and only stops when it encounters a `break`.
+
.{java-package}/stringinswitch/StringInSwitch_Break.java
[source,java,indent=0]
----
include::{section-java-package}/stringinswitch/StringInSwitch_Break.java[tag=code]
----
+
.sa铆da no console
.console output
[source,console]
----
Janeiro
N茫o 茅 um m锚s
Fevereiro
January
Not a month
February
----
+
Nesse caso a execu莽茫o inicia no `case "jan"`, passar pelo `default` e pelo `case "fev"` at茅 parar no `break`, por isso as 3 strings aparecem no console.
In this case, execution starts in `case "jan "`, goes through `default` and `case "Feb"` until it stops at `break`. So the 3 strings will appear in the console.

. Um `switch` vazio 茅 v谩lido, mesmo que n茫o tenha utilidade.
. An empty `switch` is valid even if it has no use.
+
.{java-package}/stringinswitch/StringInSwitch_Empty.java
[source,java,indent=0]
----
include::{section-java-package}/stringinswitch/StringInSwitch_Empty.java[tag=code]
----

. Todos os valores de `case` precisam ser constantes, ou seja, vari谩veis finais em tempo de compila莽茫o. Se o valor do `case` puder mudar em tempo de execu莽茫o, o c贸digo n茫o compila.
. All `case` values must be constant, i.e., final variables at compile time. If the value of `case` can change at runtime, the code does not compile.
+
.{java-package}/stringinswitch/StringInSwitch_ConstantOnly.java
[source,java,indent=0]
----
include::{section-java-package}/stringinswitch/StringInSwitch_ConstantOnly.java[tag=code]
----

Pronto, essas s茫o as regras de `switch`. Voc锚 provavelmente j谩 conhe莽e algumas referentes 脿 vers玫es anteriores do Java, mas agora voc锚 as viu em `switch` que utilizam Strings. Isso n茫o era poss铆vel antes do Java 7.
These are the switch rules. You probably already know some about previous versions of Java, but now you have seen them in `switch` that use Strings. This was not possible before Java 7.

==== Literais Bin谩rios e Num茅ricos, incluindo underscore( _ )

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ public class StringInSwitch_Break {
// tag::code[]
public static void main(String[] args) {

String mes = "jan";
String month = "jan";

switch (mes) {
switch (month) {
case "jan":
System.out.println("Janeiro");
System.out.println("January");
default:
System.out.println("N茫o 茅 um m锚s");
case "fev":
System.out.println("Fevereiro");
System.out.println("Not a month");
case "feb":
System.out.println("February");
break;
case "mar":
System.out.println("Mar莽o");
System.out.println("March");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ public class StringInSwitch_Complete {
// tag::code[]
public static void main(String[] args) {

String mes = "jan";
String month = "jan";

switch (mes) {
switch (month) {
case "jan":
System.out.println("Janeiro");
System.out.println("January");
break;
case "fev":
System.out.println("Fevereiro");
case "feb":
System.out.println("February");
break;
case "mar":
System.out.println("Mar莽o");
System.out.println("March");
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
public class StringInSwitch_ConstantOnly {

// tag::code[]
private static final String FEV = "fev";
private static final String FEB = "feb";
private static String jan = "jan";

public static void getNomeMes(final String mai) {
public static void getMonthName(final String may) {

String mes = "jan";
String month = "jan";

final String mar = "mar";
String abr = "abr";
String apr = "apr";

switch (mes) {
case jan: // N脙O COMPILA - jan 茅 um atributo comum, pode mudar em tempo de execu莽茫o
System.out.println("Janeiro");
switch (month) {
case jan: // WON'T COMPILE - jan is a common attribute, can change at runtime
System.out.println("January");
break;
case FEV: // COMPILA - FEV 茅 uma constante em tempo de compila莽茫o, seu valor nunca muda
System.out.println("Fevereiro");
case FEB: // COMPILES - FEB is a compilation time constant, its value never changes
System.out.println("February");
break;
case mar: // COMPILA - mar 茅 uma constante em tempo de compila莽茫o, seu valor nunca muda
System.out.println("Mar莽o");
case mar: // COMPILES - mar is a constant at compilation time, its value never changes
System.out.println("March");
break;
case abr: // N脙O COMPILA - abr 茅 uma vari谩vel comum, pode mudar em tempo de execu莽茫o
System.out.println("Mar莽o");
case apr: // WON'T COMPILE - apr is a common variable, can change at runtime
System.out.println("April");
break;
case mai: // N脙O COMPILA - mai 茅 final, mas n茫o 茅 constante, pode mudar em tempo de execu莽茫o
System.out.println("Mar莽o");
case may: // WON'T COMPILE - may is final but not constant, may change at runtime
System.out.println("May");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ public class StringInSwitch_Default {

// tag::code[]
public static void main(String[] args) {
String mes = "jan";
switch (mes) {

String month = "jan";

switch (month) {
case "jan":
System.out.println("Janeiro");
System.out.println("January");
break;
default: // COMPILA - O default pode estar em qualquer posi莽茫o
default: // COMPILES - `Default` can be in any position
break;
case "jan": // N脙O COMPILA - J谩 existe o case "jan"
System.out.println("Janeiro2");
case "jan": // WON'T COMPILE - There is already case "jan"
System.out.println("January2");
break;
case "mar":
System.out.println("Mar莽o");
System.out.println("March");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class StringInSwitch_Empty {
// tag::code[]
public static void main(String[] args) {

String mes = "jan";
switch (mes) {} // COMPILA - switch pode estar vazio, mesmo que seja in煤til
String month = "jan";
switch (month) {} // COMPILES - switch may be empty even if it's useless
}
// end::code[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ public class StringInSwitch_Type {
// tag::code[]
public static void main(String[] args) {

Long mes = 1L;
Long month = 1L;

switch (mes) { // N脙O COMPILA - Long n茫o 茅 um tipo suportado
switch (month) { // WON'T COMPILE - Long is not a supported type.
case 1L:
System.out.println("Janeiro");
System.out.println("January");
break;
case 2L:
System.out.println("Fevereiro");
System.out.println("February");
break;
default:
break;
Expand Down

0 comments on commit 3d91f17

Please sign in to comment.