Java Beginning Basics

For a recent code challenge, I essentially needed to learn Java in a matter of days. Not that that is actually possible, so let me rephrase: “start learning Java in a matter of days”. Because it’s an older language that has lots of particulars and coming from Ruby which does a lot of things *magically*for you, I was a bit worried, needless to say. While the two are very different, the fact that they are both object-oriented programming paradigms is helpful when transitioning from one to the other.

 

 

But those differences do come into play. Java is a statically typed language where Ruby is a dynamically typed one — where “type” refers to data types. When a programming language is statically typed, it means that all variables must be declared before use. They do not have to be defined or assigned, but their type must be declared at the outset and subsequently cannot be changed later one, although you can write code instructing the machine to read it in a different manner.

 

Theres Something Else Going On GIF

 

For example, if you need a boolean, a string, and two numbers (one with decimals and one without), it would look something like this in Java:

 

String helloWorld = “Hello World!”;
double howWellDoYouKnowJavaFromOneToTen = 4.25;
boolean butDoYouEnjoyIt;
int howManyVariables;

 

The first two types have been both declared and assigned whereas the latter two have only been declared, but it is imperative that any type of data is preceded with it’s type. In the above example “String” is the type, “helloWorld” is the name of this string, and “Hello World!” is it’s value, with each other type following a similar pattern. An important thing to note is that String is capitalized in Java because it is a class in the same way Object is [also capitalized], and boolean, int, and double are all primitive double types. For exact numbers, you use int, and for floating numbers you use double. Doubles can reach a higher range than ints based on a 32-bit system (more info can be found here: primitive types and values). A final takeaway is that semicolons “;” are read in Java and are necessary at the end of each line of code you write (most of the time, if/then statements are an exception). 

 

C and C++ are other famous statically typed languages.

 

A dynamically typed programming language, on the other hand, is one where data types do not have to be declared when creating variables, but need to be defined in order to be used. Variables can also subsequently be changed based on reassignment. So for the same variable creation as above, but in Ruby, you’d have:

 

hello_world = “Hello World!”
how_well_do_you_know_ruby_from_one_to_ten = 8.75
and_do_you_enjoy_it = null
how_many_variables = “”

 

Here, all variables are declared and assigned in the same line. For the latter two, which are currently of an “unknown” value, they are defined with null and “”, representing empty content. Both can easily be reassigned later, even the final variable, which currently is a string, can be reassigned as a number, as easily as:

 

and_do_you_enjoy_it = true
how_many_variables_defined = 4

 

Kazoo Kid Wow GIF - KazooKid Wow Amazed GIFs
JavaScript, Python, PHP, and Perl are other popular dynamically typed languages

 

Each has their pros and cons but what it essentially comes down to is runtime. Statically typed languages are ones that must be compiled before they are run. So if you write a program in a language like Java, you have to first compile your program to make sure everything is suitable before you can can run it for output. This is why the explicit declaration is necessary: the system needs to know exactly what it will be required to do before program execution. Or else it won’t run. This would also be where you’ll be notified of errors in your code. If a program is unable to be compiled, errors will be returned, and you will have to resolve them before you can run the program. Dynamically typed languages are interpreted as they’re executed. There is no need to compile a program beforehand, and no need to explicitly state data types of variables or outline every single minute step of the program as the system will interpret the lines of code as the program is run, with errors returned at runtime. This typically makes writing in an interpreted language a little bit faster because you can eliminate some of the semantics, but it can also lead to errors with misinterpreted variables.

 

 

To round it all out, I’ll include a HelloWorld.java program to complete the final two variables and show a little more of what I mean by everything must be explicitly stated.

No more of this:

def hello_world
  hello_there_world = "Hello There, World!"
  print hello_there_world
end

or even this:

def hello_world(variable)
  return variable
end

hello_there_world = "Hello There, World!”
hello_world(hello_there_world)

 

And much more of this:

(Note: This is also a little more involved than it necessarily needs to be for the purpose of one method, but I wanted extra practice.)

public class HelloWorld {

  String helloString = "Hello There World!";
  double howWellDoYouKnowJavaFromOneToTen;
  boolean butDoYouEnjoyIt;
  int howManyVariables = 4;

  public HelloWorld(double javaKnowledgeScale, boolean enjoyOrNah) {
    howWellDoYouKnowJavaFromOneToTen = javaKnowledgeScale;
    butDoYouEnjoyIt = enjoyOrNah;
  }

  public static void main(String[] args) {
    HelloWorld helloThereWorld = new HelloWorld(4.25, true);
    System.out.println(helloThereWorld.helloString);
    System.out.println(helloThereWorld);
    System.out.println("(all in all, we should see " + helloThereWorld.howManyVariables + " variables within 3 lines printed.)");
  }

  public String toString() {
    String javaRatings = "Your Java knowledge is currently a " + howWellDoYouKnowJavaFromOneToTen + " out of 10 and saying you enjoy it would be " + butDoYouEnjoyIt + "!";
    return javaRatings;
  }

}

 

With the above code saved in file titled HelloWorld.java (class name must match file name), running the following two terminal commands

javac HelloWorld.java
java HelloWorld

without errors, should result in this output printed to the terminal screen:

 

JAVA-HelloWorld

 


Started from the bottom, now we here — HELLO JAVA!

 

Sources:

Leave a comment

Filed under Uncategorized

Leave a comment