Enough Java for an Internship

Marc Rovirola
May 16, 2020 · 9 min read

After more than 300 YouTube tutorials, 3 books and countless hours practicing, I’ve now gotten an internship to spill out my knowledge and… it did not go as expected. I thought the more I knew, the better. I learned about all the possible things I could: Object, Polymorphism, Sockets, Seralizing, and much more. Have I used it all? Actually, no.

I’ve heard that only 20% of what you learn is really useful, that… is pretty much right. Below I have listed and somehow given a little explanation plus examples to everything I have used repeatedly, and what you should really practice and get good at to get an internship and succeed at it. This is only a guide and a personal opinion, your experience may vary but I feel that it is important for people to know and get out of this infinite loop of learning but not applying. Trust me, I’ve been there sucking all the YouTube videos, thinking I was getting really good.

You should look at this as a guide, not as a tutorial. You should complement what you see here with, more research, and lots of practice.

(I am assuming that you have some understanding on Java or any Object Oriented programming language. You can try part of the code on this online compiler, or your favorite compiler. Mine is Visual Studio Code!)

Here is a list of the topics I mention:

  1. Variables (String, Boolean and Integer)
  2. Loops (for loop and while loop)
  3. If else statements
  4. Classes and Methods
  5. Data Structures (ArrayList and HashMap)
  6. Access Modifiers (public and private)

Learn the basics

To learn the basics, books are great, or even YouTube tutorials. Even though these are basics, it’s all you really need. If you can really master these you are set, you are building the ground for your future in programming.

Variables

Java is known for this, all variables have to be determined by its type. There are a lot of variables names but I think the main ones are Strings, Boolean and Integer. Below I’m listing an example on how to initialize them.

String name = "John";
Boolean validation = false;
Integer cost = 10;

Of course there are many more, especially with numbers you will probably need Double, Float, Decimal, etc.

Loops

There are 3 types of loops: for loops, while loops and do-while loops. In my personal experience I’ve only used for loops.

For Loops

What is the structure of a for loop?:

for(int i=0; i<3;i++){ 
}

It starts with the variable you want to loop through. In this case it’s “i”, as it’s java you have to specify the variable type, in this case it’s an integer (int). Next, we see the condition, which is: keep looping until i is less than 3 (i < 3). Finally, we have how we want the variable to behave after each loop: (i++) this mean i = i + 1, so after each loop, increment “i” by 1. You can also use i — (i= i-1).

There is an other type of for loop which is very useful. Let’s say we have an array containing two Strings with values [“Banana”, “Apple”] and we want to go through it:

String[] listOfStrings = {"Apple", "Banana"};//for(varType varName: listYouWantToLoop)for(String str: listOfStrings){
System.out.println(str)
}
Output:
>>"Banana"
>>"Apple"

In this case we want an Object String called str to get the value of each item in listOfStrings. Let’s go step by step. On the first iteration str will take the value of “Banana”, when we parse the str value to the print, it will return our first String of the list. Same with “Apple”, the str will equal its value and print it.

How would you do the same with the first kind of for loop?

for(int i = 0;i<2;i++){
listOfStrings[i]
}

What would happen if listOfStrings had only 1 value? The java compiler would send out an error. With this type of for loops you have to be careful with the size of the data structures.

While Loop

I’ll do a quick introduction to a while loop, since I haven’t used it much.

How do you use it?

//while(condition == true) -->continueint i = 0;
while(i <3){
System.out.println(i)
i++;
}
Output:
>>0
>>1
>>2

It’s as easy as you see it. While “i” is less than 3, whatever code is inside {} will keep looping. So it will do 2 loops. (Notice the i++ is after the System.out.println, if it was the other way round the 0 wouldn’t be displayed) First loop will increment i to 1, second loop will increment i to 2, and as i takes a value of 3 the condition will be false and stop.

If else statements

An if else statement is very easy but very powerful. Let’s say you have to check if the username inputted is correct.

if(username.equals("John")){
//let him in
System.out.println("Welcome back, John");
} else {
//kick him out!!
System.out.println("Access denied!");
}

In the previous example only if username is “John” he will be granted access, any other input will go directly to the else{} block and deny his access.

Classes and Methods

Understanding how to use classes and methods is fundamental. A class is a blueprint of an Object you want to describe. Think of it like this: How would you describe a book? You would probably describe it with its title, author and number of pages. In this case the class would be Book, the properties would be the variables and the methods would be the way to obtain the variable values. Look at the following example:

public class Book{
private String title;
private String author;
private Integer numOfPages;

public Book(){ //Constructor
this.title= "";
this.author= "";
this.numOfPages = 0;
}

public void setTitle(String title){
this.title= title;
}

public String getTitle(){
return this.title;
}
public void setAuthor(String author){
this.author= author;
}

public String getAuthor(){
return this.author;
}
public void setNumOfPages(Integer numOfPages){
this.numOfPages= numOfPages;
}

public String getNumOfPages(){
return this.numOfPages;
}

}

Why should you add “public Book(){}”?

This method is named constructor. A constructor is telling you what values the Object (Book in this case) will take when it’s initialized. If the variables are not initialized you are on the way for a null value error. They are not nice… trust me.

this?

It took me a while to understand the meaning of this. Once you create a new Book, this will become your new Book Object and will take the values I've assigned. So this is a reference to the object you are managing.

Let’s create a new book:

Book harryPotter4 = new Book();

When the compiler goes through and arrives at “new Book()”, harryPotter4 will become “this”. So harryPotter4 will start with the values:

  • title = “”
  • author = “”
  • numOfPages = 0

Now, how do you assign new values or return the current ones? This is done with the setter and getters. As the name says you set or get the value. Lets change the title of harryPotter4:

harryPotter4.setTitle("Harry Potter and the Goblet of Fire");

Now if you type :

harryPotter4.getTitle();Output:
>>"Harry Potter and the Goblet of Fire"

Return types and Methods

Methods can return values. It depends on what the method is meant to do. If you don’t want your method to return anything you would use “void”. The return type goes after the access modifiers (public, private, more information below).

What else can you return in a method? Anything! You can return a String, Boolean, Book, etc.

public class MethodExamples{
public String getName(){
return "John";
}
public Boolean validName(String name){
Boolean valid = false;
if(name.equals("John"){
valid = true;
}
return valid;
}
public Book createBook(String title, String author, int numOfPages){
Book newBook = new Book();
newBook.setTitle(title);
newBook.setAuthor(author);
newBook.setNumOfPages(numOfPages);
return newBook;
}
}

When each of these methods are used they will return a value which you can store or use.

How do you use them?

First you have to call the class that the method belongs to. In this case it’s MethodExamples:

MethodExamples methodExamples = new MethodExamples();methodExamples.getName();Output:
>>"John"
Boolean valid = methodExamples.validName("Marc");
System.out.println(valid)
Output:
>>false

Calling the class MethodExamples its exactly the same as we did when creating a new Book. You are getting an instance of that Object with all of its properties and methods.

Access Modifiers (public and private)

There are 4 types: public, private, protected and default. I’ve really only used public and private. So lets define them.

  • Public: All variables, methods, constructors, setters, getters, etc. with a public access can be used across you project.
  • Private: Variables, methods and constructors with a private access will only be accessible from the class they belong too.

There will be an example at the end of this article.

Data structures

Just like variables, you have a lot of Data Structures…. I mean a lot, and you can get overwhelmed learning all of them. The main ones I’ve used for the past 6 months are Lists and Maps, more specific ArrayLists and HashMaps. The latter are sub-classes that implement List or Map, but have their own properties.

ArrayList

An ArrayList it’s a list that can dynamically grow or shorten its size. It comes with the java.util package.

List<Book> listBooks = new ArrayList<Book>();

As you can see, it’s always good practice to use the List class to start with and then specify the type of list (ArrayList). For data structures you always have to specify the type of variable you will fill it with. In this example I choose it to be a list of books with <Book>. Now let’s add a couple more books.

We create 2 books:

Book harryPotter3 = new Book();
harryPotter3.setTitle("Harry Potter and the prisoner of Azkaban");
harryPotter3.setAuthor("J.K. Rowling");
harryPotter3.setNumOfPages(435);
Book harryPotter2 = new Book();
harryPotter2.setTitle("Harry Potter and the Chamber of Secrets");
harryPotter2.setAuthor("J.K. Rowling");
listBooks.add(harryPotter2);
listBooks.add(harryPotter3)

I didn’t assign any number of pages to harryPotter2. What will happen here? As I said before, once its created it will get the constructor values I’ve set. So if we tried to get the number of pages of harryPotter2 what would we get?

harryPotter2.getNumOfPages();Output:
>> 0

We would get 0, because this.price was initialized as 0. If I hadn’t created the constructor it would have returned a Null Value Error. Pheew!

HashMap

A HashMap works with a key-value pair. Like a dictionary, you have your word (key) and the description (value). As always, I think an example is the best way to understand. Let’s say you have an extensive List of books, and you would like to access each Book by its name. I’ll follow my example with the List of Books I created in this article (3 Harry Potter Books):

//Map<key,value> mapEx = new HashMap<key,value>;Map<String, Book> mapBooks = new HashMap<String, Book>;
for(Book book: listBooks){
mapBooks.add(book.title(), book);
}
mapProducts.get("Harry Potter and the Goblet of Fire");

This way when you call the map and get a book by its name, you can find out all its information (with getters): title, author and number of pages.

Final Example

To sum it up let’s create an example of a Library Page. Let’s imagine the library page has a filter to look up the books, in this case books with more than 300 pages, and you want to store them in a filteredList to show in the web page:

https://gist.github.com/marcrovi/74595b22b9b66a878ca08f5dcb3e793b

(For some reason the embedded gist is not working, click on the link!)

I used a private access for the validate method because I am using it only in this class and I don’t want other classes to be able to access it. If you try to access this method from another class you won’t be able to find it. I used a public access for the filterBooks method so I can call it any other Class.

In this case, only books that have a number of pages greater than 300 will be added to the filteredList. If we pass our listBooks that contains 4 harryPotter Book objects that I have created along this guide, one of them have number of pages equal to 0 and one has less than 300, so they wouldn’t be added. The final result of the filteredList would be:

>>Harry Potter and The Chamber of Secrets
>>Harry Potter and The Prisoner of Azkaban

(You can post this code in your favorite compiler, and run it. Change the methods and add new ones to practice.)

I would like to point out that what I’ve talked about here are only basics and it’s for people that are going into the programming world and want to start with Java. There is plenty more I could have added but this is a list of all I have personally learned to be essential. I think to be able to advance in your career, or even start it, you should focus on the foundations, it is much easier to build on top of it. What should you do after you master the basics? Start a project! It might seem reckless to jump straight to a project but hear me out. Find a tutorial to build a web application or a mobile application, learn what files you need to create (Bean, ViewBean, Controller, Mapper, etc.) and the role of each one. Add functionality to your app and this way you will have to learn new skills and apply them right away, you will most likely have to repeat them and you’ll be able to retain them.

Happy Coding!

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

 by the author.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Marc Rovirola

Written by

Physics Student and a Coding Enthusiast!

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Marc Rovirola

Written by

Physics Student and a Coding Enthusiast!

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store