Java convention with regard to code format [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Is it allowed in Java convention to write such code:
public void nameOfTheFunction()
{
}
A lot of people said me that it is prohibited according to Java code convention and that my code should look like:
public void nameOfTheFunction(){
}
But I did not find any info about this.

This is oracle(/java) convention
see oracle
Quote:
Class and Interface Declarations
When coding Java classes and interfaces, the following formatting rules should be followed:
• No space between a method name and the parenthesis “(“ starting its parameter list
• Open brace “{” appears at the end of the same line as the declaration statement
• Closing brace “}” starts a line by itself indented to match its corresponding opening
statement, except when it is a null statement the “}” should appear immediately after the
“{“
class Sample extends Object {
int ivar1;
int ivar2;
Sample(int i, int j) {
ivar1 = i;
ivar2 = j;
}
int emptyMethod() {}
...
}

It's personal preference, plain and simple.
There are many people who tout their preferred style as the "one and only", but in reality it doesn't matter.

This is the Google Java styleguide: Google Java Style Guide
This is the Oracle styleguide: Oracle Java Style Guide
It doesn't really make a difference. You can do what you like the most. In Java however most people write the bracket on the same line as the rest (I personally prefer this as well :)

Related

Is there a way to use python and java in the same program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to use both python and java in the same program. Since the print() function of python is better, but java's int variable; is more efficient.
If I'm interpreting correctly, you want to use to use both interchangeably in the same file, so you'd end up with code like:
def main():
int x = 5;
print(x)
This is impossible, because there would be ambiguity when trying to interpret code if you allowed constructs from both languages. For example, "X" + 1 is allowed in java, and would give you the string "X1". In python, it would give you an error because you can't add an int to a string. This would mean that there would be no way to know what your code should do because it's runnable in both languages.
This is a problem that all of us face, where we like some parts of some languages and other parts of other languages. The solution is pretty much just to decide what's most important, choose one language based on that, and then put up with the parts you don't like.
You can use Jython, which is a Python implementation based on the JVM/JDK. This allows calling between Java and Python code in both directions.

why is it necessary to write (char) while printing pattern with character in java programming language [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
class Pattern4 {
public static void main(String args[]) {
for(int i=1;i<=5;i++) {
for(int j=1;j<=5;j++) {
System.out.print((char)(i+64));
}
System.out.println();
}
}
}
In this program if I don't provide () to char keyword, I am getting a compile time error. So my question is why is it necessary to write (char) and then (i+64) and why not char(i+64)?
(i+64)
is an int, because it's the sum of an int and an int.
If you want to print it as an int, you don't have to do anything.
System.out.println(i + 64);
If you want to print it as a char, you have to convert it to one:
System.out.println((char)(i + 64));
That's simply the syntax Java uses for casting.
Because char(65) is method call syntax. You're not calling a method named char with 65 as a parameter, you're casting 65 to a char.
Remember that, unlike spoken languages, programming languages are carefully designed to avoid ambiguity of any kind. This is both for "user friendliness" (i.e. to make the code clearer) and from the practical necessity of writing compilers and creating correct software. Anything that would introduce ambiguity could also "break" compilers and cause subtle, difficult-to-track bugs.
(char)int is syntax to type cast. This syntax does not take any input and its as per language design (Its not a function but language syntax). This tells compiler to accept the type as char and not as int. Java is strongly typed language and it ensures type safety.
We need to tell compiler explicitly when we need to change type otherwise compiler will complain.
Hope this clarifies why this does not take input in parenthesis like:
abc(input);

Java Arbitrary Expression Evaluator? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Write the Java code for an arbitrary expression evaluator (supporting _, -, *, /). The - and / operator only works on two operands, the others can have any number or operands. The / operator will additionally check that there is no 0 in the second operand. If it does, then it will throw a BadArithmeticException.
Write the code using the Composite pattern. Also write a client class that will create objects and calculate expressions to demonstrate the use of the composite pattern. The common method in the composite hierarchy is called eval. Here is the signature for eval.
public int eval() throws BadArithmeticException { ...
Not really sure where to begin here. Any help would be appreciated.
This isn't really a question but I can offer a suggestion at least. I would start by trying to write your exception so that you understand what you should be doing. You can find this in the Oracle docs here:
http://docs.oracle.com/javase/tutorial/essential/exceptions/creating.html
If you know that you can't divide by 0, think about what kinds of expressions a user (or you) could input that would cause a BadArithmeticException.

Are class names allowed to be lower case [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I ran my program successfully when declaring a class name starting with a lower case letter. I don't understood why it is asked to start with first capital letter.
You can declare it with lower case, but the convention is to start with a capital letter. The conventions were created to make it easier on others to read and understand your code.
Unlike this definition :
class someClass
{
otherClass b = null;
}
Sticking with the conventions even helps Stack Overflow color your code in a way that makes it more readable :
class SomeClass
{
OtherClass b = null;
}
It's not a matter of can but rather a matter of should. Java naming conventions dictate that class names should begin with an upper case letter. By following conventions, others (including us and your instructors, bosses and co-workers) can better understand and evaluate your code. If you need our help in the future with your code, this can make a big difference. There can be some local variability in some specific rules, so you will want to learn and follow all the specific rules of your office / school.
For more on this, please see Java Naming Conventions.
Also, some code editors/IDEs will hyphenate or space out related generated code file names based on capitalization in your class file.
For instance, Android Studio(https://developer.android.com/sdk/installing/studio.html) will read a Java Activity's class name, and insert a hyphen or underscore when you transition from a capital to a lower case letter for the file name of the layout.
An example: When creating a new activity(which is just a new class) called "MyActivity.java", Android Studio will also create a new layout file called "activity_my.xml" and link to it in the java file.
By sticking to the convention of capitalizing your class names, not only is your source code easier for others to follow and learn, but it will be much easier for you to navigate and keep track of files in your project. Naming conventions are everything.
Nothing happens if class names are lower case or upper, as long as the the code runs, then it shouldn't be a problem.
Its doesn't matter whether you choose uppercase or lower case but just for code to be readable without ambiguity.

Where to put braces in Java While loop? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I understand that Java ignores indentation and that curly-brace hierarchies are recommended to increase readability of code. I come from Python with decent experience and I am learning Java right now. I am completely new to Java and I don't yet know the "Good Coding Practice" that comes with writing while loops.
Here are 2 ways to create a basic while loop:
while ( booleanCondition = true ) {
// do stuff
}
And the second way:
while (booleanCondition = true )
{
// do stuff
}
I am not one for splitting hairs over the number of lines of code, so the fact that the first loop is one line shorter is irrelevant to me. Personally, I like the second better because the loop is left-justified with only the condition on the top line. However, I don't want to start using this format if it is not recommended for Good Practice. Is the first way to do the while loop more/less recommended? What is the most common format if there is one?
Both works. This is actually based more towards the programmer's preference and style.
The second one is better since the bracket should start right below its name. It would be least confusing if you use nested loops or conditions. Anything nested would go one level inner and you would never make mistake of brackets and code would be perfectly readable.
while (booleanCondition = true )
{
//do stuff
while (booleanCondition = true )
{
//do stuff
}
}
In this you perfectly know which bracket is ending where. Every bracket ends right below it and there are no brackets in between. Simple and elegant style of coding.
Two schools of thought:
1) In 1997 Sun published a set of "Coding Conventions" for Java. They specify pretty much everything you can think of when it comes to writing Java code - indentation, variable naming, etc, etc: http://www.oracle.com/technetwork/java/codeconventions-150003.pdf . Follow those rules.
2) Do it however you'd like, but keep it consistent. There's any number of styles, etc out there (See: http://en.wikipedia.org/wiki/Indent_style) - pick one, use it in all files in a project.
Java conventions prescribe the first method.
7.6 while Statements
A while statement should have the following form:
while (condition) {
statements;
}
This is also the most commonly used one.
But in the end, it's up to yourself. Just keep it consistent within the project.
It depend on the developer , but basically java doc and IDE prefer the 1st option.
Also if the booleanCondition is boolean you dont need to check == with true :
while (booleanCondition) {
// do stuff
}

Categories

Resources