Skip to main content

java learning first step: in comparison to python

Introduction:

I didn't start my career with java, but with python and a bit of C. Then years after, I am learning java for android projects. In this blog, I will point out how I am learning java and also a little bit of resources and information along the way to help you learn. 

This is more for a person who already is familiar with the programming concepts but is getting accustomed with the java syntax. Also it only spans to very basic and if people like it then only I will proceed with further details. I have taken guidance and screenshots from the android course by rob percieval for this blog. If you want to learn android, do take his course.

Thanks and lets begin.

Initial observations:

 (1) a java code needs to have a class to start with. Also each class has method or functions associated to a class. among these methods, it must have the main method.

(2) void means it returns nothing.

(3) static means some variable which doesn't change at all. it is only dependent on the class, and not on the instance. which also means that it will be same across instances and can't be changed.

(4) println stands for print a line.

Data types: primitive and derivatives:

primitive types are char, int, double, boolean, byte, long and short. these are the similar types we use in c, c++ or any other similar strong typed language. java is also a strong typed language, hence all the types.

But String myname = 'rob';

this assignment has string starting with capital letter as string is not a primitive type. 'String' is a char array, and it is actually a class in java. Also, java allows string concatenation simply, just like python. for example, look at this little example below:

also, primitives like int can be implicitly converted while printing unlike python or javascript. for example look into this below:

also, we can call methods of a class, even the simple ones also; just like we do in python. see how we use length() of a string using this process:


a simple task involving all the variables:

Arrays in java:

seems like that java uses curly bracket to write array. also you can't directly print this. But good thing again is that it uses 0 to start indexing.

simple array is unmutable. so you can't change them.

for using list in java, we need to import java.util.* to use all the java items. Now, we can use list by using ArrayList(). 

so it will work like this:

List listName = new ArrayList();

simply add is the method for adding items.

listName.add(1); listName.add(2); so on.

list.get(index_no) gives you the index_no element from the list.

list.remove(index_no) is a pop from python.

list.toString() is also good.

a small task:


so basically, Arraylist() is the type. add is the new append. remove is the pop. and get is for getting specific index. tostring is a method not there in python ( at least I don't know of).

oh before we move on, java, doesn't take or I will say accept single quotes. strings are not defined in java with single quotes. I find it weird but maybe that's how it is. 

Dictionaries in java:

Map map = new HashMap();

Map.put(father, "rob");

also Map allows Map.size() telling you the length of it.

Map.toString() lets you print it.

Map.remove('key') lets you remove a specific key. Basically an equivalent to del Map['key']

Rob didn't provide any tutorial for map procedure.

Now we will be learning conditionals.

If

simple if works in the following syntax:

if(condition) { //code block}

if else works in the following syntax

if(condition){//code block}else(condition){//code block}

Now, rob gave a simple task here. create a list of two integers; and then compare them and print which one is bigger.

I tried it with ArrayList but failed. It seems that I struck an error named

java.lang.object is bad operand for >


But why does this occur?

this occurs because arraylist elements are non-primitive. and you can't do operator overloading in java like in python; so you would have to use something called comparable.

A detailed description of it is provided here.

Next up: while loop:

syntax:

while(condition){code}

please consider for this type of loop that it surely ends. otherwise you would keep looping for infinite and crash browser/laptop/ide etc.

Sure. Now, let's see more.

We will try the simple "print the even numbers" question which rob gives as assignment.


interesting fact x++ works in java too. i.e. the "++" increment. So questions related to x = x+1 and x++ problems can be asked too for java interviews.

For loop:

for loop is exactly similar to the c format. the syntax is:

for(datatype variable = startpoint; variable endpoint condition; variable -- or ++) {codeblock}

see this block of code in action.

A bit more complicated example, where we solve the task given by rob to solve triangular numbers.

Conclusion:

So that's it. We are done with the basics, i.e. data types, conditionals and dictionary, list etc. Next is learning specific coding techniques and oops tricks to java; which you can start on your own. Let me know in the comments what more would you like to read about java and or specific concepts etc. Thank you for reading and stay tuned to the blog.

Comments

Popular posts from this blog

Mastering SQL for Data Science: Top SQL Interview Questions by Experience Level

Introduction: SQL (Structured Query Language) is a cornerstone of data manipulation and querying in data science. SQL technical rounds are designed to assess a candidate’s ability to work with databases, retrieve, and manipulate data efficiently. This guide provides a comprehensive list of SQL interview questions segmented by experience level—beginner, intermediate, and experienced. For each level, you'll find key questions designed to evaluate the candidate’s proficiency in SQL and their ability to solve data-related problems. The difficulty increases as the experience level rises, and the final section will guide you on how to prepare effectively for these rounds. Beginner (0-2 Years of Experience) At this stage, candidates are expected to know the basics of SQL, common commands, and elementary data manipulation. What is SQL? Explain its importance in data science. Hint: Think about querying, relational databases, and data manipulation. What is the difference between WHERE

What is Bort?

 Introduction: Bort, is the new and more optimized version of BERT; which came out this october from amazon science. I came to know about it today while parsing amazon science's news on facebook about bort. So Bort is the newest addition to the long list of great LM models with extra-ordinary achievements.  Why is Bort important? Bort, is a model of 5.5% effective and 16% total size of the original BERT model; and is 20x faster than BERT, while being able to surpass the BERT model in 20 out of 23 tasks; to quote the abstract of the paper,  ' it obtains performance improvements of between 0 . 3% and 31%, absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks. ' So what made this achievement possible? The main idea behind creation of Bort is to go beyond the shallow depth of weight pruning, connection deletion or merely factoring the NN into different matrix factorizations and thus distilling it. While methods like knowle

Spacy errors and their solutions

 Introduction: There are a bunch of errors in spacy, which never makes sense until you get to the depth of it. In this post, we will analyze the attribute error E046 and why it occurs. (1) AttributeError: [E046] Can't retrieve unregistered extension attribute 'tag_name'. Did you forget to call the set_extension method? Let's first understand what the error means on superficial level. There is a tag_name extension in your code. i.e. from a doc object, probably you are calling doc._.tag_name. But spacy suggests to you that probably you forgot to call the set_extension method. So what to do from here? The problem in hand is that your extension is not created where it should have been created. Now in general this means that your pipeline is incorrect at some level.  So how should you solve it? Look into the pipeline of your spacy language object. Chances are that the pipeline component which creates the extension is not included in the pipeline. To check the pipe eleme