Jump to content

Hello there!

 

I have recently started learning Java using codeacademy. This is the first time I have started learning coding.So far I have finished 25 lessons but still I find my self surrounded by confusions. My question is that why do we write code with lots of brackets and spaces like this? 

public class Mouse extends Rodentia {

	String name;

	public Mouse(String name) {

		this.name = name;

	}

	public void eat() {

		System.out.println(name + " ate some cheese pizza!");

	}

	public void solveMaze(int minutes) {

		System.out.println(name + " solved the maze in " + minutes + " minutes!");

	}

	public static void main(String[] args) {

		Mouse ratly = new Mouse("Ratly");
		ratly.eat();
		ratly.solveMaze(3);
		ratly.order();

	}

}

I will keep posting my questions in this thread. Thanks!

Link to comment
https://linustechtips.com/topic/611967-need-help-with-java/
Share on other sites

Link to post
Share on other sites

Braces delimitate code blocks, compound statements. They are part of the language syntax. Each code block also defines a local scope which is available only inside it and not outside, but that's not really related to your question.

The spaces there aren't necessary at all. I honestly think they're overused in your example, but each to their own. They are used to format the code, to make it easier to read. It could have looked like this just fine :

public class Mouse extends Rodentia {
	String name;
	public Mouse(String name) {
		this.name = name;
	}
	public void eat() {
		System.out.println(name + " ate some cheese pizza!");
	}
	public void solveMaze(int minutes) {
		System.out.println(name + " solved the maze in " + minutes + " minutes!");
	}
	public static void main(String[] args) {
		Mouse ratly = new Mouse("Ratly");
		ratly.eat();
		ratly.solveMaze(3);
		ratly.order();
	}
}

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/611967-need-help-with-java/#findComment-7910945
Share on other sites

Link to post
Share on other sites

You could write it all in a single line too, it wouldn't make much of a difference, other than that it would look like shit.

This is just someone's personal preference and it's helpful to keep your code organized. I personally like to do my code kind of like this:

function DoSomething()
{
 	//Do stuff 
}

but it's all up to you.

 

And the brackets are just the way the language works. It's like a quote mark in English, you open a quote with it and end a quote with it. Just like the bracket open and close the function.

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/611967-need-help-with-java/#findComment-7910975
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×