I didn't understand java boolean - boolean

I'm new at java and I didn't understand boolean very well. Can someone explain boolean?
What is it?
Where will I use it?
Why ı use?

A boolean value is one with two choices: true or false, like yes or no, 1 or 0.
boolean isSunday= true;
So instead of typing int or double or string, you just type boolean (with a lower case "b"). After the name of you variable, you can assign a value of either true or false.
This is how to use. By the way, boolean's value is false default in Java.
boolean isSunday= true;
if ( isSunday ) {
System.out.println("it's Sunday!");
}
else {
System.out.println("it's not Sunday");
}

What is it?
boolean is a data type to represent true/false value.
Where will I use it?
In your java programs for instance a flag to know if an item is found in list or not.
Why ı use?
To save memory and enhance readability.

Related

Swifty way of naming a boolean property

1.
According to the swift API design guidelines, a boolean property should read as assertions

> Uses of Boolean methods and properties should read as assertions about
the receiver when the use is nonmutating,
e.g. x.isEmpty, line1.intersects(line2).
2.
I would like to make a computed property of which type is Boolean to the existing data type.
Here is a simplified version of my code:
struct State {
var authorID: String
var myID: String
var `XXX`: Bool {
return myID == authorID
}
}
I want the property XXX to stand for whether I am author or not.
I firstly came up with the names like authorIsMe, iAmAuthor, isAuthorMe, etc. but realized that it didn’t read as assertions about the receiver.
So, what name do you think fit best for XXX? Any idea will be appreciated.
Thank you
(Please do not consider inlining the expression myID == authorID because in the original code, it is not short as above so I need the computed property)
amITheAuthor is the best property name according to me as it will clearly throw the answer & its means of use , its a suggestion you can use this as well.

What is the purpose of ValidationResult.Success field?

Msdn:
public static readonly ValidationResult ValidationResult.Success
Represents the success of the validation (true if validation was
successful; otherwise, false).
The text in above excerpt doesn't make sense to me, since Success field doesn't return a value of type bool, and the value it does return ( ie ValidationResult instance ) doesn't contain any boolean property or field which we could set to a value indicating a success or failure of a validation?!
Any ideas what is the purpose of this field?
ValidationResult.Success is always constant null. Its purpose is documentation.
In order to succeed validation you could either write:
return null;
or
return ValidationResult.Success;
In the first case I ask myself "What does this mean? What does null mean? Is this success, or fail, or something else?". In the latter case the code is inherently documented without the need for informal text docs.

What is prefered to use record.getAttribute or record.getAttributeAsTYPE?

I'm using MySQL for dev environment but my app could be on SQLServer on staging environment.
In my database, booleans (boolean) are record as VARCHAR, integers (int) are record as VARCHAR too.
Are the methods record.getAttributeAsTYPE like getAttributeAsBoolean(java.lang.String) appropriate / sure to use ? Or is it better to use something like type myvar = new TYPE(record.getAttribute("COLUNM_NAME")) ?
What is better to use between solution 1 and 2 ?
Example 1 :
boolean mybool = record.getAttributeAsBoolean("COLUNM_NAME"); // Solution 1
boolean mybool = new Boolean(record.getAttribute("COLUNM_NAME")); // Solution 2
Example 2 :
int myint = record.getAttributeAsInt("COLUNM_NAME"); // Solution 1
int myint = new Interger(record.getAttribute("COLUNM_NAME")); // Solution 2
For me, VARCHAR may be closer to java.lang.String than java.lang.Boolean or java.lang.Integer. So I think "Solution 2", in these examples, are more sure.
It depends. If you are absolutely certain that the value of the attribute is of the type you require, you should use getAttributeAsTYPE(). How is your data source (if you're using ds.xml) defining that field? It just might be converted implicitly by smartGWT.
Looks like your value is stored as String (ex: record.setAttribute("COLUMN_NAME", "12345"), calling record.getAttributeAsInt("COLUMN_NAME") will likely fail with the type exception. In that case, the only thing you can do is instantiate Integer or convert it statically: Integer.parseInt(recrod.getAttribute()).
getAttributeAsTYPE is convenient and I use it as much as I can but you have to be sure that the value is of the right type when being set.

Convert type of KnockOutJs.linkObservableToUrl mapped value to bool

I'm working on single page application, which involves sorting.
I use
viewModel = new {
SortAsc = ko.observable(true)
};
ko.linkObservableToUrl(viewModel.SortAsc, "Asc", viewModel.SortAsc());
to achieve that mapping. And it works, but the problem is that mapping returns literal strings "false" and "true" instead of bool value. This causes a problem with checkbox, which is bound to that property:
<input type="checkbox" data-bind="checked: SortAsc" value="Ascending"/>
The question is, how can I make that value from url to be converted to correct type (normal bool), so my checkbox will be updated properly?
Ok, I found how to overcome that problem. Not very elegant, but works.
1. I assumed, that SortAsc will be a string property in my logic. So I left it bound to url like in the question text. Only initialized it with string, istead of bool ("true" intead of true).
2. I created writeable dependend observable, which will do the convertion:
viewModel.SortAscBool = ko.dependentObservable({
read: function () {
return this.SortAsc() === "true";
},
write: function (value) {
this.SortAsc(String(value));
},
owner: viewModel
});
and bound my checkbox to that prop. So now, when checkbox is checked, SortAscBool is changed and it sets literal value to SortAsc (I think this convertion is really not needed, but as a C# programmer I like it that way :)). And of course, when SortAsc changes, SortAscBool will also change and return the converted value to checked binding. And that is what was really needed.
Also, my first though was to simply create one way dependend observable, but then url will not be updated with values from checkbox.

Is there a better way to test for an integer in C# than Double.TryParse?

Double.TryParse returns a value, and I don't need a value. I need to be able to tell if a string is numeric and just return a bool.
is there a way to do this?
I would consider exactly what you need to determine. "Is numeric" is vaguer than it sounds at first. Consider the following strings, and whether you'd want to consider them numeric:
"NaN"
"Nan"
"Infinity"
"0.000000000000000000000000000000000000000000000000001"
"1e5"
"1e500"
"1,000"
"+1"
Using Double.TryParse (with an en-GB culture - don't forget about cultural issues!) will give you True, False, True, True (despite it not being representable), True, False. True, True.
If you want to tell whether a later call to Double.TryParse would succeed, calling it here will be the most accurate solution. If you're using some other criteria, a regular expression may well be more appropriate. An example of the criteria you might use:
The can be a + or -, but only in the first character
There can be a single period at any character. You may want to avoid one at the end - should "1." be valid?
Other than the above, all characters must be digits
That would disallow all but the fourth and last examples above.
EDIT: I've now noticed the title of the question includes "integer". That pretty much reduces the specification checks to:
Do you want to allow leading zeroes (e.g. -00012)?
What is the range?
Do you only need decimals (instead of hex etc)?
Do you need to accept thousands separators?
What's your policy on leading/trailing whitespace?
Well, you could use a regular expression, but why not just discard the value from Double.TryParse and move on? I don't think it will be worth the effort trying to duplicate this code.
One way is to add a reference to Microsoft.VisualBasic and then use Information.IsNumeric().
using Microsoft.VisualBasic;
...
if (Information.IsNumeric("1233434.0"))
{
Console.WriteLine("Yes");
}
How about a regular expression?
string s = "13.2";
bool bIsDecimal = Regex.IsMatch("^-?\d+(\.\d+)?$");
should test whether it is a decimal value. What it won't tell you is whether it is a valid decimal, as in, will the number fit in the range of a decimal.
I just fired up Visual Studio Express (both 2005 and 2008). The Intellisense says that the return value of Double.TryParse() is a bool. The following worked for me under limited testing...
double res; // you must be under very resource-constrained
// conditions if you can't just declare a double
// and forget about it
if (Double.TryParse(textBox1.Text, out res)) {
label1.Text = "it's a number";
} else {
label1.Text = "not a number";
}
try this isnumeric:
public static bool IsNumeric(object Expression)
{
bool isNum;
double retNum;
isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any,System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
return isNum;
}