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.
Related
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 4 years ago.
Improve this question
I am trying to finish an assignment in my intro to Java course, and I have some questions. First off, what does it mean when there is a -- in FRONT of an int value? Also what is a String Builder? I had some help but want to understand what it is I'm using in the code. Thanks.
The -- in front of a value simply means subtract 1 from it. Similarly, ++ in front of a value means add 1 to it.
If you write ++
before the number it is called prefix operator and if after then its post fix
preFix: ++a will increase the value before using it, will first increase and then use it.
postFix a++ will first use it and then use it, for later use you will get the incremented value.
-- is a predecrement
Java StringBuilder class is used to create mutable (modifiable) string.
A String is immutable i.e string cannot be changed once created and everytime a value is change it create new string.
But in case of StringBuilder which is mutable string can change.
My experience is mostly with C# not Java, but in C# strings cannot be changed, when you concatenate two strings like "hello" + "world" you do not change either string, you create a new one and the old two still exist. If you need to do this many times (dozens or hundreds) it can use a lot of memory. A StringBuilder allows you to conserve memory by appending characters to the same block of memory while you are building your string, and then you can turn the result into a normal string for passing around to other functions.
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 :)
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 7 years ago.
Improve this question
hi i have a java programming assignment wich include 3 exercice i have done 2 already but in the third one i am dont know wich data type i should use
here is the question :
Write a java program that reads from the user the course code, section and the scores of a student for three exams representing two midterms and a final. The percentage of each of the three exams is of fixed value 30, 30 and 40 percent respectively. Your program computes the final average grade and displays it along with the course code and the section.
Remark: All data, except for the average, must be whole numbers and you should use the most efficient data type that is suitable for this specific exercise.
Sample Run:
Enter your course code: CSCI250
Enter your section: E
Enter the scores of the tests and the final: 97 83 77
CSCI250 E 84.8 (result)
so what i want to know is what is the preferable data type to use for course code ? and char is the one that i should use for section right ?
If you're capturing user input, use a String for everything.
Reason? You may request a number, but the user can type anything. Your code needs to handle bad input.
I think you can use String as data type for Course Code. You can write both numbers and letters by using it. And for section, yes, char will be suitable for it.
Use a String for arbitrary text.
If the section code is always present1 and is never more than character than a char can be used.
However, I would still use a String for consistency, flexibility, and easy of use. The teacher may prefer this based on the "efficient"cy they are going for.
1The is a soft "always": while char cannot represent null a sentinel (eg. '\0' or ' ') can be used to indicate 'no section specified'. Using such a sentinel to supplement null can also (but does not always) lead to more logic work - in particular when the record is displayed.
In any case, it is probably best to not switch to Character just for the null as this is most likely outside of the scope of current course work.
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.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
What significance does a letter after a number have? For example the bellow table for primitive types in Java has letters after the default values of long float and double.
I tested and as far as I can tell they never make a difference. I've also seen things like this in C and C++, for example how the NULL macro sometimes expands to 0L.
It does make a difference. It just depends on what your values are.
First and foremost, Java will treat all integral declarations as an int unless you specify the L (or l) suffix.
This means that, while this declaration is invalid:
System.out.println(5_000_000_000_000); // too large for an int
...this declaration would be:
System.out.println(5_000_000_000_000L);
Java will also treat all floating-point declarations as a double unless you specify f or F. You could also specify d or D, but this is an optional and implied declaration that your literal type is a double.
Another example: while this declaration is valid for a double:
System.out.println(1.17e200);
...this one isn't:
float f = 1.17e200f; // too large
The behavior for other languages (C, C++) would be specific to which standard you're using, but it's not quite what you're thinking - a macro is simply a pre-compiler text replace, so wherever the compiler sees NULL, it would replace it with 0.