Exploring Java : My Learning Adventure
Table of contents
Installation
Welcome!! If you are new to Java, kindly follow the instructions given here. Let's start!
Likewise Python, Java is also a versatile, object-oriented programming language. To code in Java, you need some tools to create an environment for it. For example, we need a JDK (Java Development Kit). Yes, you can code Java in Visual Studio Code (VS Code). I recommend watching YouTube tutorials for a step-by-step guide for the process. It's easier than following a written instructions. You will find plenty of YouTube tutorials that goes with your needs. However, here's the link of two videos I found useful so thought of embedding them cause why waste your time for searching when your instructor could provide you links.
To get Java from Oracle - Download JDK
VS code tutorial on Java - VS Code, Java
Tips :
Try to get the version with "LTS"(Long Term Support) when choosing a Java version, means it's a stable version with fewer bugs. You can install older LTS versions too. Oracle releases a new version every 6 months, so older versions are really not that old.
If you are installing and preparing setup to run Java on your pc, install the stuffs on you C:
After installing, open your command prompt and write "java - version" to check if it has been really installed or not. Cmd plays a vital role to check java issues.
However, there are online IDEs for Java. You can try them out too, such as - Replit. Moreover, for more efficiency, you should actually apply for GitHub student developer pack. Here's the link to apply for it https://education.github.com/pack
Very Very important !! If you don't want to get frustrated, please note down these things
After running your code, if you get error message "Error: Could not find or load main class App" typically it means that the Java runtime cannot find the compiled class file in the expected directory. This could be due to incorrect file paths, directory structure, or a mismatch between the class name and the file name.
Ensure the Correct Directory Structure and File Name:
Your class is named
First
, so your file should be namedFirst.java
.The directory structure should be such that
First.java
is located directly under thesrc
folder.Hover and right click on
src
and make a new file and name it same as your project name. Also, don't write anything intoApp.java
That's basically a java basic format for your convenience, got it?
[ Do you know why I am mentioning these before even landing on the code? Because I struggled with this exact same error, causing me one whole day to understand and fix the error. Hence, I really don't want anyone to write a code that was supposed to print 25
but instead it printed Hello meaw meaw
, like seriously! It's ridiculous ]
Skeleton of Java
Okay so, there are some built-in packages in Java. To utilize their features, you have to import them in your code. In Java, java.lang
is a fundamental package that is automatically imported and it will automatically import even if you don't write it.
Too many unknown words I know! Let's start one by one
Class
In Java, all code must be inside a class, and the class name should be same as file name. If you are using command prompt as compiler than you have to write "javac <file name>" (c means compiler) and the compiler will check the code for errors, and if found none, it will generate an error free bytecode file with the same name as the class. To run the program, use
java <class name>
. This command calls the Java Virtual Machine (JVM), which converts the bytecode into machine code and executes the program.main
methodInside the class, there is a main method named
main
, which is the entry point of the program. Here the execution begins. The main method has avoid
return type, indicating that it does not return any value.public
If you want anything from the class to be accessible from outside, you have to make it visible so that JVM can see it. The JVM calls the
main
method, hence it should bepublic
to be accessible.static
You can't create a class if you don't have an object. However,
static
is used when there are no objects. It enables a program to run without object just by calling the class. If you don't writestatic
, it will show error.Command-line arguments
They are just parameters passed to the main method. They are stored in the
String[] args
. Although it is optional, you should always write theString[] args
parameter in the main method, regardless of whether you plan to pass arguments or not. IncludingString[] args
in the main method is considered a best practice for flexibility and consistency with Java standards.System.out.println("Your message here");
If you want anything to be displayed on screen, you put it inside quotes in the
println
method. Theprintln
method is part of theout
object, which belongs to theSystem
class.
Started with capital letter means it is a class. if it has brackets, that means it is a method. And if it has nothing, that means it is an object. And the shown example is basically the core structure to all your Java codes, alright?
In the next blog, i will talk about Reading from keyboard and Data types.
In the meantime, don't forget to stay hydrated! Happy learning!
Subscribe to my newsletter
Read articles from Fatima Jannet directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by