Techno Blender
Digitally Yours.

Variables and Data Types in Java. Java for Data Science part 1 | by Philip Wilkinson | Nov, 2022

0 50


Java for Data Science part 1

Photo by Caspar Camille Rubin on Unsplash

Java, as a language, often doesn’t appear in the list of languages to learn for Data Science. Often that debate is left to those arguing that either R or Python is the best language to start off with. But, since Data Science encompasses a wide range of fields and applications, it means that one language cannot always do everything, or some languages can be more useful in other areas. Java then is one of those languages that works best in some subdomains of Data Science, with a particular focus on applications of big data, due to integration with Hadoop, and IoT infrastructure and applications, because of its portability. This, along the fact that it can be found in many different areas beyond Data Science as well. This makes it handy to learn if you want a language that is used in big data or IoT applications or a language that goes beyond just Data Science! So if you are interested in learning it, continue below where we cover the basics of Variables and Data Types in Java.

Printing

One of the first things to learn then, in any language, before going into how to create and use variables, is how to output results. This makes it clear what we are doing and how the language is behaving which is important when getting started with a new language. While this is a relatively simple in command in Python with print() , in Java, to print out something to the console we have to use:

System.out.println();

Which is the instruction for the computer to display something on the screen.

Breaking it down, this is a system command ( System ), that presents text as an output ( out ) by printing it on a screen in a new line ( println() ), and to end any line of code in Java we need to add a ; .Thus, printing out anything we want!

For example:

System.out.println(2+3);

will print out 5!

Creating and using variables

Now that we know how to print out the results of anything that we do, we can then cover how to create and use variables. Each variable in Java has a name and a value, just like in any other language. While the name of the variable won’t change, the value can.

The main difference for Java though, as opposed to another language like Python, is that Java is statically typed. This means that you have to define what type of information that the variable will store, such as integers, strings or other data types. Be careful, as this cannot be changed later on!

To code a variable then in Java, we can declare it, by stating the name and the Data Type that it is to contain. For example, we can declare a variable called passengers that will contain only integer values.

int passengers;

This tells the computer to create a variable called passengers, that in the future we will only assign integers to it.

We then initialise the variable when we assign a value to it. To start of with then, we can assign a value of zero passengers:

int passengers;passengers = 0;

So we now have defined a variable called passengers, told the computer that we will only assign integer values to it, and initialised it by assigning 0 to the variable.

Once the variable has been initialised, it can then be manipulated or edited to what we want. In this case since we have defined an instantiated an integer variable, we can perform all the standard mathematical manipulations we can in other languages. For example:

int passengers;passengers = 0;passengers = passengers + 5;passengers = passengers - 3;passengers = passengers - 1 + 5;

Which when printed out to the console will display:

System.out.println(passengers);#out:
6

Strings

The same way we declared an integer variable and instatiated it, we can also declare a string variable. To declare a string we use:

String driver

Which, as with an integer, tells the computer we want to declared a variable called driver that will hold a string.

This can be then be instantiated in the same way by assigning it a value as:

String driverdriver = "Hamish";

This, as with the integer, gives us the power to manipulate the string as we would in other languages and extract information from it. For example:

// define and initialise the string
String driver;
driver = "Hamish";
// We can count the number of items in the string and
// assign the value to another variable
int letter = driver.length();// we can capitalise all the lettersdriverUpper = driver.toUpperCase();// and we can add strings togetherString driverFirstName = "Hamish";String driverLastName = "Blake"String driverFullName = driverFirstName + " " + driverLastNameSystem.out.print(driverFullName);#out:
Hamish Blake

The only thing we did differently here for the driverFirstName and driverLastName variable is that we declared and instantiated it on the same line.

Data types

We have seen how to create a string and an integer, then what other data types do we have in Java?

  • Byte ( byte ): Stores whole numbers but only between -128 and 128
  • Short ( short ): Stores whole numbers but only between -32,768 to 32,767
  • Integer ( int ) : A whole number that can have a maximum of up to 10 digits, but which has benefits of speeding up the defined program
  • Long ( long ): A whole number but can be much longer than 10 digits, but at the cost of taking up more space and potentially slowing the program down
  • Double ( double ): Can be used to store anything including integer, decimal, positive or negative but can slow the program down
  • String ( String ): Yep, a string!
  • Character ( char ): Which stores a single letter e.g. char answer = "b";
  • Boolean ( boolean ): Used to store true or false (true!)

Each of these have to be declared and then initialised as with both the integer and string have done above.

Naming conventions

In naming these variables, it is important to both stick to the rules and also convention.

The first rule is that names should start with a letter. While they can start with a - or a $ , it is not recommended to do as this is bad convention. Also, names are case sensitive, so make sure whatever case you use, you use it consistenly.

The second rule is that names should be defined using lowerCamelCase for consistency and ease of reading. This ensures that words are clearly separated and defined. No whitespace can be used in variable names!

The third and final rule is that names should always use full words to describe what it is used for! Try to limit the abbreviations if possible, unless they are consistently used and have been clearly defined, as you never know who is going to read your code!

Finally, while not necessarily a naming convention, we can comment out lines of code using either // to comment out a single line, or part of a line after the notation, or we can add a multiline commne between /* multi line comment*/ .

Conclusion

Java is a widely used language beyond Data Science, but within Data Science it is often used for Big Data applications or IoT. While it is the big hitters or either R or Python, it still does have some use! With this practical you should be able to get started on your journey learning Java, whether that is for fun or a new project you are working on. Good luck!


Java for Data Science part 1

Photo by Caspar Camille Rubin on Unsplash

Java, as a language, often doesn’t appear in the list of languages to learn for Data Science. Often that debate is left to those arguing that either R or Python is the best language to start off with. But, since Data Science encompasses a wide range of fields and applications, it means that one language cannot always do everything, or some languages can be more useful in other areas. Java then is one of those languages that works best in some subdomains of Data Science, with a particular focus on applications of big data, due to integration with Hadoop, and IoT infrastructure and applications, because of its portability. This, along the fact that it can be found in many different areas beyond Data Science as well. This makes it handy to learn if you want a language that is used in big data or IoT applications or a language that goes beyond just Data Science! So if you are interested in learning it, continue below where we cover the basics of Variables and Data Types in Java.

Printing

One of the first things to learn then, in any language, before going into how to create and use variables, is how to output results. This makes it clear what we are doing and how the language is behaving which is important when getting started with a new language. While this is a relatively simple in command in Python with print() , in Java, to print out something to the console we have to use:

System.out.println();

Which is the instruction for the computer to display something on the screen.

Breaking it down, this is a system command ( System ), that presents text as an output ( out ) by printing it on a screen in a new line ( println() ), and to end any line of code in Java we need to add a ; .Thus, printing out anything we want!

For example:

System.out.println(2+3);

will print out 5!

Creating and using variables

Now that we know how to print out the results of anything that we do, we can then cover how to create and use variables. Each variable in Java has a name and a value, just like in any other language. While the name of the variable won’t change, the value can.

The main difference for Java though, as opposed to another language like Python, is that Java is statically typed. This means that you have to define what type of information that the variable will store, such as integers, strings or other data types. Be careful, as this cannot be changed later on!

To code a variable then in Java, we can declare it, by stating the name and the Data Type that it is to contain. For example, we can declare a variable called passengers that will contain only integer values.

int passengers;

This tells the computer to create a variable called passengers, that in the future we will only assign integers to it.

We then initialise the variable when we assign a value to it. To start of with then, we can assign a value of zero passengers:

int passengers;passengers = 0;

So we now have defined a variable called passengers, told the computer that we will only assign integer values to it, and initialised it by assigning 0 to the variable.

Once the variable has been initialised, it can then be manipulated or edited to what we want. In this case since we have defined an instantiated an integer variable, we can perform all the standard mathematical manipulations we can in other languages. For example:

int passengers;passengers = 0;passengers = passengers + 5;passengers = passengers - 3;passengers = passengers - 1 + 5;

Which when printed out to the console will display:

System.out.println(passengers);#out:
6

Strings

The same way we declared an integer variable and instatiated it, we can also declare a string variable. To declare a string we use:

String driver

Which, as with an integer, tells the computer we want to declared a variable called driver that will hold a string.

This can be then be instantiated in the same way by assigning it a value as:

String driverdriver = "Hamish";

This, as with the integer, gives us the power to manipulate the string as we would in other languages and extract information from it. For example:

// define and initialise the string
String driver;
driver = "Hamish";
// We can count the number of items in the string and
// assign the value to another variable
int letter = driver.length();// we can capitalise all the lettersdriverUpper = driver.toUpperCase();// and we can add strings togetherString driverFirstName = "Hamish";String driverLastName = "Blake"String driverFullName = driverFirstName + " " + driverLastNameSystem.out.print(driverFullName);#out:
Hamish Blake

The only thing we did differently here for the driverFirstName and driverLastName variable is that we declared and instantiated it on the same line.

Data types

We have seen how to create a string and an integer, then what other data types do we have in Java?

  • Byte ( byte ): Stores whole numbers but only between -128 and 128
  • Short ( short ): Stores whole numbers but only between -32,768 to 32,767
  • Integer ( int ) : A whole number that can have a maximum of up to 10 digits, but which has benefits of speeding up the defined program
  • Long ( long ): A whole number but can be much longer than 10 digits, but at the cost of taking up more space and potentially slowing the program down
  • Double ( double ): Can be used to store anything including integer, decimal, positive or negative but can slow the program down
  • String ( String ): Yep, a string!
  • Character ( char ): Which stores a single letter e.g. char answer = "b";
  • Boolean ( boolean ): Used to store true or false (true!)

Each of these have to be declared and then initialised as with both the integer and string have done above.

Naming conventions

In naming these variables, it is important to both stick to the rules and also convention.

The first rule is that names should start with a letter. While they can start with a - or a $ , it is not recommended to do as this is bad convention. Also, names are case sensitive, so make sure whatever case you use, you use it consistenly.

The second rule is that names should be defined using lowerCamelCase for consistency and ease of reading. This ensures that words are clearly separated and defined. No whitespace can be used in variable names!

The third and final rule is that names should always use full words to describe what it is used for! Try to limit the abbreviations if possible, unless they are consistently used and have been clearly defined, as you never know who is going to read your code!

Finally, while not necessarily a naming convention, we can comment out lines of code using either // to comment out a single line, or part of a line after the notation, or we can add a multiline commne between /* multi line comment*/ .

Conclusion

Java is a widely used language beyond Data Science, but within Data Science it is often used for Big Data applications or IoT. While it is the big hitters or either R or Python, it still does have some use! With this practical you should be able to get started on your journey learning Java, whether that is for fun or a new project you are working on. Good luck!

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment