Are class names allowed to be lower case [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 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.

Related

Java convention with regard to code format [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 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 :)

Java conventions for field name as keyword [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 6 years ago.
Improve this question
Is there any convention for fields with name same as java keyword?
For example want to create field with name "public":
public class Event{
private boolean public_;
}
The general convention (nothing official, but for experience) in this cases is to add the name of the class as a suffix.
private boolean publicEvent
There is no standard convention since the code convention document released by sun in 1999.
The only reference to variable names on that document is the following (chapter 9):
Except for variables, all instance, class, and
class constants are in mixed case with a lowercase
first letter. Internal words start with capital
letters.
Variable names should be short yet meaningful.
The choice of a variable name should be
mnemonic— that is, designed to indicate to the
casual observer the intent of its use. One-character
variable names should be avoided except
for temporary “throwaway” variables. Common
names for temporary variables are i, j, k,
m, and n for integers; c, d, and e for characters.
As you can see there is no reference to how to use a keyword as variable name. The only tip that I can suggest are:
avoid it and use a different word (a synonym)
if you choose to use a keyword, be coherent and use the same convention for the whole code (for example prefix it with _ or suffix it with the name of the class)
To shorten the name consider combining first letter of class with keyword, in your case:
ePublic
Hope it helps.

When is is appropriate to have multiple java statements on one line? [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 8 years ago.
Improve this question
I've seen code that looks like this:
String name;
Scanner scan = new Scanner(System.in);
System.out.println("Enter your name"); name = scan.nextLine();
As opposed to:
String name;
Scanner scan = new Scanner(System.in);
System.out.println("Enter your name");
name = scan.nextLine();
Which one is generally regarded as better practice?
The usual convention is to put each statement on its own line. According to Google's style guide:
4.3 One statement per line
Each statement is followed by a line-break.
The second snippet in your question complies with this rule. Following conventions makes it easier for people to read each others' code.
Note you can use an IDE to format your code (CTRL+SHIFT+F in Eclipse) and it will move this around for you.
Regardless of standards, following the style used in existing code is usually a good idea. Especially, reformatting in the same check-in as code changes makes it hard to see what changed when bringing in new versions.
In a team situation, formatting is not the place to express your individuality. Use the same formatting as the other people on the team. Reformatting wars between team members who have their IDEs set with different formatting rules are really annoying.
Also, not declaring variables until they're used is the preferred style. Declaring a local variable without assigning a value means it can't be used until something assigns it a value anyway.
You should always try to make your code as readable as possible. And generally, your code is more readable the better formatted it is. Placing two statements on the same line is usually seen as harder to read then having each statement get it's own line.
The rule (yahoo) when modifying; code exactly like (in the style of) the programmer who wrote the original code. When writing new code code exactly like the other programmers on the team. If it's just you then whatever you think is the most readable. Keep in mind some source indentation tools will separate multi statement lines.
Your example seams to make a little more sense to me if coded in-line.
String name = scan.nextLine();
Either of course, are allowable. This comes down to personal preference and what you see as more readable. Seeing that a program is written out line by line, and there's logic in each line, I'd opt for the second one. It makes it a little easier to understand what's going on, and easier to debug in my eyes. Performance wise, there's absolutely no difference between the two. To more directly answer your question, in my eyes, it's never appropriate.

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
}

Is introducing underscores in literals in java beneficial or a drawback? [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
Though oracle technotes state that :
In Java SE 7 and later, any number of underscore characters (_) can
appear anywhere between digits in a numerical literal. This feature
enables you, for example, to separate groups of digits in numeric
literals, which can improve the readability of your code.
example : float pi = 3.14_15F;
is same as
float pi = 3.1415F;
But does it not become confusing to the developers working on code written by someone else?
Also does the use of underscore put any overhead on compiler or not?
But does it not become confusing to the developers working on code written by someone else?
Only if the developers don't understand the Java language! This construct has been been supported for long enough that every Java professional should recognize it ... even if they don't use it in their own code.
On the other hand, if your Java developers have not bothered to keep up to date with the new things in Java 7, they may be (temporarily) baffled. But the real solution is to educate your developers.
Also does the use of underscore put any overhead on compiler or not?
The overhead would be so small that it is impossible to measure.
There is no performance issue here.
The only time it would make any sense to use underscores is in a very large integer or with a binary integer. Like almost any bit of syntactical freedom the language provides, people are free to misuse it and write difficult to read code. I doubt this underscore thing will become a problem any more than the freedom to add extra white space is a problem.
The best example for when you would want to use this is with binary numbers where it is customary to place a space between every 4 bits.
For instance, compare:
int bitField = 0b110111011111;
int bitField2= 0b1101_1101_1111; //clearly more readable.
Other examples might include a credit card number or SSN as given in Oracle's documentation of this feature.

Categories

Resources