length vs length() vs Size() vs Count() in JAVA

length:
Java uses the word length when you’re asking about
something that can be determined by telling you the distance from
the start to the end of something – thus an array, with fixed size
elements arranged sequentially in memory, is going to be a length.

 

Size():

 

Java uses size where you’re looking at a measure of the number
of elements in something which is not in successive memory locations,
or may not be, or where the elements themselves differ in size so you can’t find out
how many of them there are by measuring the distance from start to end.

 

Length():

It is a method defined in String class. It gives the number of characters .

Count():
Count() tends to refer to the number of elements in a looser collection.

size = available slots, count = actual elements. size == count when the collection is full.

 

package com.me.t24;

import java.util.ArrayList;

public class llSCC
{
public static void main(String[] args)
{
String[] stringNames=new String[10];
System.out.println(stringNames.length);
//In Java, array (not java.util.Array) is a predefined class in the language itself.
//To find the elements of an array, designers used length variable
//(length is a field member in the predefined class).
//it prints 10

String string=”always”;
System.out.println(string.length());
//It is a method defined in String class. It gives the number of characters
//present in the string
//6

//why to use length over lenth();

/*The reason is by performance, executing length variable is speedier than calling the method
length(). It is like comparing two strings with == and equals(). equals()
is a method call which takes more time than executing == operator.*/

ArrayList<String> arrayList=new ArrayList<>();
arrayList.add(“asd”);
arrayList.add(“fh”);
arrayList.add(“dfrgf”);

System.out.println(arrayList.size());
//Size() tends to refer to the size of the collection,
//3

}
}

 

10
6
3

Advertisements

Quotes-Keep calm and Quote your Experience:

Don’t listen to anyone…Don’t ever listen my  words because my loved ones knews naked truth about me i.e how much bad iam..

 

Fallow Your Heart whatever the situation, You have another chance  except  if it’s our death ..Whatever the situation favour/not   make your own decision which makes you feel good about yourself  and MAKE THE HISTORY with your own attitude.

     —Always Madhu,A Failure guy.  

 

It’s not an idea can change life….It is a life can change many ideas actually.

 

Don’t ever give a fuck to the ones who loves you and your loved ones…because they are the physical proofs of your true character.

 

Don’t be naked to someone …Y is it hurts so much when they left with marks of their memories on us..It’ll be scar forever.. .

 

Since the moment we were born..No matter how rich you are, how happy you are, Life gives us the fuck in terms of human relationships at every  moment at everytime till death.We have to bare this folks so that we can get the shit group for survival which results nothing at death.

 

There are two types of people exits around us in industry.
1.The ones who actually do the things
2.The ones who manages the things like they did it actually though actually not.
Second ones are most respected,most paid than first ones..But
always be in first species..because always second ones are directly propotional to the output of first ones.

 

 

To get the GOOD …Firstly,YOU have to be  GOOD enough..

 

Be like a question that never been answered..that never can be answered..

 

Only one person we owe to is Mother..

 

Death is such a such a great thing, It certains everything poor,Rich,Intelligent,Good,Bad etc…
It certins all..all means everything …did u got it –it includes shit also.. .

 

People usually lives their respective lives in 3 ways with respect to you:
1.With You
2.Without you
3.Lives with your memories in your absence.
Fall Under any of three..If not Your death is worth enough than your life.

 

Every Stuff in the world is a subset of the Nature.It’s our destiny…

 

Make the difference with your own attitude and passion..No matter what it results either GOOD or BAD..
Keep try to generate the impact on other’s lives ,Keep touching the lives …
If not, Your death will not generates any imapct on other ‘s lives.

 

 

We can compete/we should compete with the ones who were made by mankind.We shoudn’t compete with the
natural powers and resources like GAALI,NEERU,NIPPU,NELA,AKASAM ..It would results an unexpected stuff
that we can’t take.. So use them responsibly because nature doesn’t need the shit like respect it needs only
responsibility.

 

Evrey creature birth it self is a fucking damn stuff’s result.
Now you can realize the necessity of fucking shit……right?????
So accustomed yourself to it.
But make sure that your life is not ended up like a shit inorder to find the shit.

 

Life makes us uncertain at every time though we are living happly with all prosperity.
It’s because we hope better life day by day..which ruins our happiness..But without hope your life
is worthless…To maintain in_balance b/w these two we are wasting our lives..

 

I’m Inferior…You need Y iam because iam an inferior… .

 

Knowngly or unknowngly we encourages our death by growing ourselves in day to day life..

 

Purity is not lies in How loud you are saying “I love u”…It lies in the taste of your tears
for your loved ones…

 

To be call as Creature you don’t anything except our birth..
To be call as human being you need clothes apart from first one..
To be call as Gentle human being you need compassion ….Though you don’t have first and second
“You Are The Great”

 

There is no long lasting relattionships exists in cosmoc except relationship with nature..All
other relationships are subjective to chage according to their respective needs.

 

If your not willing to change..don’t live better die…

 

 

There are no such personalities like Good human being,Great man,Worse woman…It’s all about
generating impact on other’s lives by choosing/doing it in a GOOD/BAD way.
GOOD/BAD it’s just a simple option that everyone have at any point of time..But it makes
us feel GOOD/BAD about our legacy.
GOOD ones touches other’s lives..BAD ones becomes un_touchble.

 

There is no such thing like time..It’s all about transforming our thoughts into actions in
order to survive better..Attitude is everything.. .

 

 

Whatever the fucking moment GOOD/BAD/SAD/HAPPY etc..
I always choose teras……..
Always Tears: 1.I was cried when i was born
2.I cried when my loved ones with me i.e sweet tears.
3.I cried when beloved left me..
4.Though i laugh whole heartedly sweet teras comes out of my eyes
5.peopel who are there for me will really cry when i was dead

Purity lies in tears..because sad eyes never lies.. .

read() ,read(byte[] b) and read(byte[], int offset, int length) in InputStream class in JAVA

package io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class First {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream=null;

if(fileInputStream==null){
try {
fileInputStream=new FileInputStream(“C:\\Users\\Always Madhu\\Desktop\\p.txt”);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

int i=0;
while((i=fileInputStream.read())!=-1){
System.out.print((char)i);

}

byte[] bs=new byte[fileInputStream.available()];
for(int i1=0;i1<bs.length;i1++){
System.out.println((char)bs[i1]);
}

 

byte b1[]=new byte[fin2.available()];
fin2.read(b1,0,b1.length);
System.out.println(“\n\nThird way\n”);
for(int i=0;i<b1.length;i++)
{
System.out.print((char)b1[i]);
}

}
}

AtomicInteger Class and All its Methods With Examples in JAVA

Refer Here………….link

 

package com.me.ocjp;

import java.io.Console;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerDemo {
public static void main(String[] args) {

//constructors
AtomicInteger atomicInteger=new AtomicInteger();
atomicInteger.set(123);
System.out.println(atomicInteger);

AtomicInteger atomicInteger2=new AtomicInteger(246);
System.out.println(atomicInteger2);

System.out.println(“—————————————“);

//get() set()
atomicInteger.set(552);
System.out.println(atomicInteger.get());
atomicInteger2.set(579);
System.out.println(atomicInteger2.get());

System.out.println(“—————————————“);

//compare and SET
AtomicInteger atomicInteger3=new AtomicInteger(571);
System.out.println(atomicInteger3);

Scanner scanner=new Scanner(System.in);
System.out.println(“Enter the Number:”);
String output=scanner.next();

//if we enter other than 571 atomicInteger3 value dsnt change
//if input==571 then mentioned 125 will be updated.
atomicInteger3.compareAndSet(Integer.parseInt(output), 125);
System.out.println(atomicInteger3);

System.out.println(“—————————————“);

//Week compare and SET
AtomicInteger atomicInteger30=new AtomicInteger(552);
System.out.println(atomicInteger30);

Scanner scanner1=new Scanner(System.in);
System.out.println(“Enter the Number:”);
String output0=scanner1.next();

//if we enter other than 552 atomicInteger3 value dsnt change
//if input==552 then mentioned 1024 will be updated.
atomicInteger30.weakCompareAndSet(Integer.parseInt(output0), 1024);
System.out.println(atomicInteger30);

System.out.println(“—————————————“);

//Add and Get and ++,after add

AtomicInteger atomicInteger4=new AtomicInteger();
atomicInteger4.set(123);
System.out.println(atomicInteger4);

//Get——>———–>then adds +specified no——-returns—->
System.out.println(atomicInteger4.addAndGet(100));
System.out.println(atomicInteger4);

//Get——>retuns———–>then adds +specified no———–>
System.out.println(atomicInteger4.getAndAdd(100));
System.out.println(atomicInteger4);

//Get——>retuns———–>then adds +1———–>
System.out.println(atomicInteger4.getAndIncrement());
System.out.println(atomicInteger4);

//Get——>———–>then adds +s1——-returns—->
System.out.println(atomicInteger4.incrementAndGet());
System.out.println(atomicInteger4);

//sub and Get and –,after sub

AtomicInteger atomicInteger5=new AtomicInteger();
atomicInteger5.set(123);
System.out.println(atomicInteger5);

//Get——>———–>then adds -1 ——-returns—->
System.out.println(atomicInteger5.decrementAndGet());
System.out.println(atomicInteger5);

//Get——>retuns———–>then adds -1 no———–>
System.out.println(atomicInteger5.getAndDecrement());
System.out.println(atomicInteger5);

System.out.println(“————————————–“);

AtomicInteger atomicInteger6=new AtomicInteger();
atomicInteger6.set(123);
System.out.println(atomicInteger6);

//diff types of data representations
System.out.println(atomicInteger6.byteValue());
System.out.println(atomicInteger6.doubleValue());
System.out.println(atomicInteger6.floatValue());
System.out.println(atomicInteger6.intValue());
System.out.println(atomicInteger6.shortValue());

System.out.println(“———————“);

//—-return old value—–and sets new value—-123 552->
System.out.println(atomicInteger6.getAndSet(552));
System.out.println(atomicInteger6.get());

//it adds the atomicInteger6 value +709 and saves it to atomicInteger6
System.out.println(atomicInteger6.accumulateAndGet(709,(a, b) -> a + b));
System.out.println(atomicInteger6);

//——–return previous value—then adds the value and saves to object.
System.out.println(atomicInteger6.getAndAccumulate(709,(a, b) -> a + b));
System.out.println(atomicInteger6);

System.out.println(“—————————-“);

AtomicInteger atomicInteger7=new AtomicInteger();
atomicInteger7.set(123);
System.out.println(atomicInteger7);

//updates value by solving exp —perform add/mul/sub then
//saves to obj
//return result
System.out.println(atomicInteger7.updateAndGet(i->i*100));
System.out.println(atomicInteger7);

//return result
//updates value by solving exp —perform add/mul/sub then
//saves it to obj
System.out.println(atomicInteger7.getAndUpdate(i->i*100));
System.out.println(atomicInteger7);
}
}

 

123
246
—————————————
552
579
—————————————
571
Enter the Number:
571
125
—————————————
552
Enter the Number:

552
1024
—————————————
123
223
223
223
323
323
324
325
325
123
122
122
122
121
————————————–
123
123
123.0
123.0
123
123
———————
123
552
1261
1261
1261
1970
—————————-
123
12300
12300
12300
1230000

Why Float.compare() and compareTo() methods instead of == while Comparing two float values in JAVA

package com.me.ocjp;

public class FloatC
{
public static void main(String[] args)
{
System.out.println(Float.NEGATIVE_INFINITY==Float.NEGATIVE_INFINITY);
System.out.println(Float.POSITIVE_INFINITY==Float.POSITIVE_INFINITY);
System.out.println(Float.BYTES==Float.BYTES);
System.out.println(Float.MIN_VALUE==Float.MIN_VALUE);
System.out.println(Float.MAX_EXPONENT==Float.MAX_EXPONENT);
System.out.println(Float.MIN_EXPONENT==Float.MIN_EXPONENT);
System.out.println(Float.MIN_NORMAL==Float.MIN_NORMAL);
System.out.println(Float.SIZE==Float.SIZE);

//Here it always returns false
//Java implements the IEEE-754 floating point standard which guarantees that any comparison
//against NaN will return false (except != which returns true)
System.out.println(Float.NaN==Float.NaN);

final float NaN;
NaN= Float.intBitsToFloat(0x7fc00000);
float x = Float.intBitsToFloat(0x7fc00000);
float y = Float.intBitsToFloat(0x7fc00000);
if(x==y)
{
System.out.println(“Equal”);
}
else
{
System.out.println(“not equal”);
}
int res=Float.compare(20.3f, 21.5f);
//is quals to new Float(f1).compareTo(new Float(f2))
//here method args are primitives
if(res==0){
System.out.println(“Equal”);
}else if(res>0){
System.out.println(“First One is Higher”);
}else{
System.out.println(“First One is Lower”);
}
Float float1=new Float(123.52f);
Float float2=new Float(56.12);
int ress=float1.compareTo(float2);
//it will takes args as Objects to compare
if(ress==0){
System.out.println(“Equal”);
}else if(ress>0){
System.out.println(“First One is Higher”);
}else{
System.out.println(“First One is Lower”);
}

}
}

 

 

true
true
true
true
true
true
true
true
false
not equal
First One is Lower
First One is Higher

 

Refer     https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#compare(float,%20float)

Integer Wrapper Class Objects Comparision in JAVA

package com.me.ocjp;

public class WrapperCompare {
public static void main(String[] args) {

//Checks if nums {-128 to 127} else it wont check in case of wrapper.
Integer i1 = 127;
Integer i2 = 127;
System.out.println((i1==i2) + ” ” + i1.equals(i2));

//Checks if nums {-128 to 127} else it wont check in case of wrapper.
Integer i11 = 17827;
Integer i21 = 17827;
System.out.println((i11==i21) + ” ” + i11.equals(i21));

//we are assignig references
Integer i3 = 21;
Integer i4 = new Integer(21);
System.out.println((i3==i4) + ” ” + i3.equals(i4));

//Checks if nums {-128 to 127} else it wont check in case of wrapper.
Integer i5 = 21;
Integer i6 = 21;
System.out.println((i5==i6) + ” ” + i5.equals(i6));

//we are assignig references
Integer i7 = 2001;
Integer i8 = i7;
System.out.println((i7==i8) + ” ” + i7.equals(i8));

}
}

 

true true
false true
false true
true true
true true

PassByValue AND PassByReference In JAVA

pls do refer………..

 

 

 

package com.me.ocjp;

import java.util.concurrent.atomic.AtomicInteger;

public class PassByValueAndReference
{
public static void main(String args[])
{

Car car1 = new Car(“BMW”);
System.out.println(“Brand of Car Inside main() before: ” + car1.car);

System.out.println(“————————“);

//if u send the Object it will reflect globally up to method() scope
//it will modifies the value if u modify the current method parameter value

printBrand(car1);
System.out.println(“Brand of Car Inside main() after printBrand: ” + car1.car);

System.out.println(“————————“);

//if u send the Object it wont reflect globally up to method() scope
//it will modifies the value if ur not modify the current method parameter value
//insteading if u create any new object reference it will be available up to method() only.
printBrandAgaian(car1);
System.out.println(“Brand of Car Inside main() after printBrandAgain: ” + car1.car);

System.out.println(“————————“);

//becoz Of All Wrapper Classes are immutable in Nature We cant modify The Value.
//but java 5 With Java 5.0, additional wrapper classes were introduced
//in the java.util.concurrent.atomic package. These classes
//are mutable and cannot be used as a replacement for the regular wrapper classes.
//Instead, they provide atomic operations for addition, increment and assignment.

Integer len = Integer.valueOf(123);
System.out.println(“len before = ” + len);
m1(len);
System.out.println(“len after= ” + len);

System.out.println(“———————————“);

//if u send the values Though they are mutable we are
//changing/ sending only value so its scope lies within () only.
//it doesn’t reflect globally up to method() scope
//it will modifies the value
int len2 = 100;
System.out.println(“len = ” + len2);
m1(len2);
System.out.println(“len = ” + len2);

System.out.println(“——————-“);

//We can change the values through objcets of respective classes
Change change=new Change();
change.var=120;
System.out.println(change.var);
change.chaneM(change.var);
System.out.println(change.var);

System.out.println(“——————–“);

// With Java 5.0, additional wrapper classes were introduced
//in the java.util.concurrent.atomic package. These classes
//are mutable and cannot be used as a replacement for the regular wrapper classes.
//Instead, they provide atomic operations for addition, increment and assignment.
//So Here We can Change the value globally bcz we are Sending OBject

AtomicInteger atomicInteger=new AtomicInteger(123);
System.out.println(atomicInteger);
m4(atomicInteger);
System.out.println(atomicInteger);

System.out.println(“——————-“);

//As Strings are Immutable We can’t change it .
String s=”Good Morining maaa…!”;
System.out.println(s);
M2(s);
System.out.println(s);

System.out.println(“—————————“);

//String Buffer is immutable and we are sending its reference
//i.e sb so changes made in () will reflect globally
StringBuffer sb=new StringBuffer(“Good Morining maaa…!”);
System.out.println(sb);
M3(sb);
System.out.println(sb);

System.out.println(“———————–“);

}
public static void printBrand(Car car2)
{
car2.car = “Maruti”;
System.out.println(“Brand of Car Inside printBrand(): ” + car2.car);
}

public static void printBrandAgaian(Car car3)
{
car3 = new Car(“Mercdes”);
System.out.println(“Brand of Car Inside printBrandAgain(): ” + car3.car);
}

private static void m4(AtomicInteger atomicInteger)
{
atomicInteger.set(321463287);
}
private static void M3(StringBuffer sb)
{
sb.append(“and Ammu”);
System.out.println(sb);
}

private static void M2(String s)
{
s+=”and Ammu”;
System.out.println(s);
}

public static void m1(Integer Length)
{
Length+=32432;
System.out.println(“inside ()”+Length);
}
}

class Car
{
String car;
public Car(String car)
{
this.car=car;
}
}

class Change{
int var;
public Change()
{

}

public void chaneM(int as)
{
this.var+=123;
}
}

 

Brand of Car Inside main() before: BMW
————————
Brand of Car Inside printBrand(): Maruti
Brand of Car Inside main() after printBrand: Maruti
————————
Brand of Car Inside printBrandAgain(): Mercdes
Brand of Car Inside main() after printBrandAgain: Maruti
————————
len before = 123
inside ()32555
len after= 123
———————————
len = 100
inside ()32532
len = 100
——————-
120
243
——————–
123
321463287
——————-
Good Morining maaa…!
Good Morining maaa…!and Ammu
Good Morining maaa…!
—————————
Good Morining maaa…!
Good Morining maaa…!and Ammu
Good Morining maaa…!and Ammu
———————–

Stop a thread in java without using stop()

package com.me.thread;

public class ThreadStop extends Thread {
private boolean b=false;
public void exit(boolean b){
this.b=b;
}
public void run() {
while(!b){
System.out.println(“Still……..”);}
}
public static void main(String[] args) throws InterruptedException {
ThreadStop tstop=new ThreadStop();
tstop.start();
Thread.sleep(5000);
tstop.exit(true);
//tstop.run();

}
}

 

 

Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..
Still……..

//prints upto 5 sec then stops execution……………..

 

 

Problem with Thread.stop() here

Passing method() as a parameter to a function in java

It is achievable through interface………

package com.trianz.collections;
interface In{
public void cusCheck(int i);
}
public class MethodPAram implements In{
@Override
public void cusCheck(int i) {
if(i>436543){
System.out.println(“its higher”);}
else{
System.out.println(“it’s lesser”);
}
}
public static void main(String[] args) {
MA(new MethodPAram(),325432543);
MA(new MethodPAram(),3543);
MA(new MethodPAram(),33);
}

private static void MA(MethodPAram methodPAram, int i) {
methodPAram.cusCheck(i);
}
}

 

output:

its higher
it’s lesser
it’s lesser

 

 

 

Pass By Value and Pass By Reference in java

For Def REFRE here….

Example Code:

package com.trianz.collections;

public class Methods
{
static int j=20;
public static void main(String[] args)
{
int i=10;
Methods methods=new Methods();
aM(i);

//here we are sending value of i only so reflections doesnt apply when
//control comes out of the method
System.out.println(i);

//It affects becoz of static keyword.
System.out.println(j);

Point point=new Point(0,0);
Point point1=new Point(0,0);

System.out.println(“—————“);

chamge(point,point1);
//Here we are passing the Object referenecs so changes made to it will reflect forever

System.out.println(point.x);
System.out.println(point.y);
System.out.println(point1.x);
System.out.println(point1.y);

}
private static void chamge(Point point, Point point1) {
point.x=1232;
point.y=4365543;

}
private static void aM(int i1) {
i1=i1*100;
j=j*100;
}
}
class Point
{
int x,y;
public Point(int i, int j) {

}

}

output:

 

10
2000
—————
1232
4365543
0
0