java coding adventure

A word on Java Types.

Variables

In Java, you use variables to store some information. Like in Calculator class you use ‘model’ and ‘producentName’ to store the information respectively about calculator’s model and producer’s name:

String producentName = "Casio";
String model="fx-350ES PLUS";

In class Potato in main() method you also have a variable:

 Calculator calculator = new Calculator(); 

Variables have a name (the word you use to refer to the value the variable contains) and a data type (which determines the kind of data the variable can store).

Variable Data Types

Data type tells you if the variable is a text, a number, a character or an object.

Objects

The line below tells you that your variable type is Calculator. It means your variable is an object of class Calculator:

 Calculator calculator = new Calculator(); 

Here you have objects of class String:

 String producentName = "Casio";
String model="fx-350ES PLUS"; 

Every data type that starts with capital letters means it’s an object build using some class. Like above String and Calculator.

Primitive Data Types

Except for objects you have simple types. They are called primitive because they are not objects.

The are eight primitive data types in Java:

  • boolean, the type whose values are either true or false
  • char, the character type whose values are 16-bit Unicode characters
  • the arithmetic types:
    • the integral types (representing integers):
      • byte
      • short
      • int
      • long
    • the floating-point types (representing real numbers):
      • float
      • double

The full list is available here.

Declaration

All the code you’ve seen in this article were the declarations of the variables:

 Calculator calculator = new Calculator();

String producentName = "Casio";
String model="fx-350ES PLUS"; 

In Java, all variables must be declared before they can be used.Typical declaration needs

Typical declaration looks like this:

Calculator calculator;
String name;
int number;
char letter;
boolean isRight;

Initialization

When you declare a variable you can store a value:

calculator = new Calculator();
name = "Aga";
number = 3;
letter = 'a';
isRight = true;

Declaration with initialization

You can also declare a value and initialize it at the same time:

Calculator calculator = new Calculator();
String name = "Aga";
int number = 3;
char letter = 'a';
boolean isRight = true;

1 thought on “A word on Java Types.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s