Bifalgorithm post · Computer science one · Declaration & Assignment

What shorthand and read-only options are available for Java variables?

What shorthand and read-only options are available for Java variables? (Computer science one · Declaration & Assignment) — "For brevity, Java allows you to dec…

Ideas in this flowchart

  • Java permite declarar variables, asignar valores y establecer valores predeterminados de forma compacta.
  • Una variable puede declararse y recibir su valor en la misma línea.
  • Ejemplos: int numUnits = 42; double costPerUnit = 32.79; char firstInitial = 'C'; boolean isStudent = true;
  • Se pueden declarar varias variables en una misma línea separándolas con comas.
  • Las variables declaradas juntas deben ser del mismo tipo.
  • Ejemplos: int numOrders, numUnits = 42, numCustomers = 10, numItems; y double costPerUnit = 32.79, salesTaxRate;
  • La palabra clave final convierte una variable en una variable de solo lectura.
  • Ejemplos: final int secret = 42; y final double salesTaxRate = 0.075;
  • Después de asignar un valor a una variable final, su valor no puede cambiar.
  • Intentar reasignar una variable final produce un error del compilador.
  • Las variables de instancia tienen valores predeterminados según su tipo.
  • El valor predeterminado para los objetos es null.
  • El valor predeterminado para todos los tipos numéricos es cero.
  • El valor predeterminado para boolean es false.
  • El valor predeterminado para char es \0, el carácter terminador nulo.
Feed/

What shorthand and read-only options are available for Java variables?

No cover

Channel

Computer science one

1 page

"For brevity, Java allows you to declare a variable and immediately assign it a value on the same line. So these two code blocks could have been more compactly written as:\n\n1 int numUnits = 42;\n\n2 double costPerUnit = 32.79;\n\n3 char firstInitial = 'C';\n\n4 boolean isStudent = true;\n\nAs another shorthand, we can declare multiple variables on the same line by delimiting them with a comma. However, they must be of the same type. We can also use an assignment with them.\n\n1 int numOrders, numUnits = 42, numCustomers = 10, numItems;\n\n2 double costPerUnit = 32.79, salesTaxRate;\n\nAnother useful keyword is final. Though it has several uses, when applied to a variable declaration, it makes it a read-only variable. After a value has been assigned to a final variable, its value cannot be changed.\n\n1 final int secret = 42;\n\n2 final double salesTaxRate = 0.075;\n\nAny attempt to reassign the values of final variables will result in a compiler error. Instance variables, that is variables declared as part of an object do have default values. For objects, the default is null, for all numeric types, zero is the default value. For the boolean type, false is the default, and the default char value is \\0, the null-terminating character (zero in the ASCII table)."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.