hi
Monday, January 3, 2022
Saturday, November 27, 2021
JAVA Tricky
JAVA Tricky Questions
- Can java overload main method?
Yes, but JVM only executes original main method. It never calls overloaded main methods
overloading method- two or more methods in a class have same method name, but must have different parameterse.g.
public class MyMain {
public static void main(int args){
System.out.println("This is a int "+args);
}public static void main(boolean args){
System.out.println("This is a boolean "+args);
}public static void main(String args[]){
System.out.println("This is main method");
MyMain myMain = new MyMain();
myMain.main(5);
myMain.main(true);
}
} - In java, can main method override?
No. In java static method cannot be overridden.
method override- same method name & same parameters in parent class & child class.
It can override from parent to child class.
private, static, final methods can't overridden, but it could be non static to static, non final to final - In Java, Can create program without main method?
Yes, we can create class without main method, but it can't be execute because of main method could not find by JVM. It will be error `No such method, "main"`
e.g.
public class MyMain{
System.out.println("This is a class");
} - Is java pure object oriented language?
No. Java is not pure object oriented language because
- java supports byte, Boolean, integers, double, float, characters primitive data type. These keywords are not objects.
-java does not supports multiple inheritance. - Why multiple inheritance not supports in JAVA?
Multiple inheritance is not supported because it leads to deadly diamond problem. Java supports multiple inheritance through interfaces only. A class can implement any number of interfaces but can extend only one class.
In multiple inheritance class can inherit the properties from one or more parent class. Same signature available in super class & child class. JVM can't determine which method gets call or which class method gets priority.
e.g.
class P1{
void callMe(){
System.out.println("I am P1"); } - what is meaning of public static void main(string[] args)?
why the declaration is standard for main method
public - Access specifier, main method allows to accessible everywhere.
static- main method to get loaded without getting by any object. It access without creating object.
void- return type. does not return any value.
main- method name
String args- String array. defining array of string passing of command line - What is static class in JAVA?
static inner class is a nested class which is a static members of outer class. It can can be accessing without instance of outer class
e.g.
class Outerclass {
public static String str = "Hello";
public static class NestedClass {
public void m1(){
System.out.println("Hello NestedClass")
}
}
public class InnerClass {
public void m2(){
System.out.println("Hello InnerClass");
}
}
}
public class Main {
public static void main(String[] args) {
//static method call
Outerclass.NestedClass nestedClass = new Outerclass.NestedClass();
nestedClass.m1();
//call non static method
Outerclass.InnerClass innerClass = new Outerclass().new InnerClass();
innerClass.m2();
//call non static method
Outerclass outerclass = new Outerclass();
InnerClass outerclass1 = outerclass.new InnerClass();
outerclass1.m2();
//static keyword call
System.out.println(Outerclass.str);
}
} - What is nested class in JAVA?
Class within calss is called nested class. Nested Class can be static or non static
e.g.
class Outerclass {
public static String str = "Hello";
public static class NestedClass {
public void m1(){
System.out.println("Hello NestedClass");
}
}
public class InnerClass {
public void m2(){
System.out.println("Hello InnerClass");
}
}
}
public class Main {
public static void main(String[] args) {
//static method call
Outerclass.NestedClass nestedClass = new Outerclass.NestedClass();
nestedClass.m1();
//call non static method
Outerclass.InnerClass innerClass = new Outerclass().new InnerClass();
innerClass.m2();
//call non static method
Outerclass outerclass = new Outerclass();
InnerClass outerclass1 = outerclass.new InnerClass();
outerclass1.m2();
//static keyword call
System.out.println(Outerclass.str);
}
}
- What is nested class in JAVA?
Class within calss is called nested class. Nested Class can be static or non static - Main difference is of == & eqaul that is operator and method.
- == is the reference comparison(address comparison) & equals is the content comparison. == is the check both objects points to same memory location.
- if class does not override the equals, then by default equals method used the closed of the parent class.
e.g.
public class EqualsMethod {
public static void main(String[] args) {
String s1 = "HELLO";
String s2 = "HELLO";
String s3 = new String("HELLO");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true
}
} - What is polymorphism?
Polymorphism is a ability of an object takes from many forms. Polymorphism means many forms. Polymorphism is a ability to display messages from different forms.
Polymorphism allows to perform single action from different ways. Polymorphism allows to implement one interface or multiple interfaces.
Like. The Person at the same time has different characteristics like husband, father, son, brother.
Two types of polymorphism
- Compile time polymorphism
- Run time polymorphism
1. Compile time polymorphism: It is also known as static polymorphism. In this polymorphism having method overloading and operator overloading.
Method overloading : method overloading is the one or more methods having same name with different arguments.
e.g.
class Test {
static int multiply(int a, int b){
return a*b;
}
static int multiply(int a, int b, int c){
return a*b*c;
}
}
class PolymorphismTest{
public static void main(String args[]){
System.out.println("Multiply := "+Test.multiply(8, 5));
System.out.println("Multiply := "+Test.multiply(8, 5, 2)); }
}
2. Run time polymorphism: This is also known as Dynamic Method Dispatch. In this polymorphism, method can overriding. Functions call overridden method resolved run time.
//compilation error
- e.g. class Parent {
- void print(){
- System.out.println("This is parent");
- }
- }
- class SubClass extends Parent{
- void print(){
- System.out.println("This is SubClass");
- }
- }
- class SubClass1 extends Parent{
- void print(){
- System.out.println("This is SubClass1");
- }
- }
- class Chid {
- public static void main(String args[]){
- Parent p;
- p = new Parent;
- p.print();
- p = new SubClass();
- p.print();
- p = new SubClass1();
- p.print();
- }
- }
Monday, March 25, 2019
Track App usage in Mobile
This functionality is already inbuilt into Android.
- Open your dailer
- Enter
*#*#4636#*#* - Select Usage statistics
- Sort by Launch Count.
Every phone or Mobile Broadband device has a unique 15 digit code, called an IMEI number.
- Enter *#06#
Will get unique 15 digit code.
Tuesday, October 16, 2018
GIT Command 1. Initialize git repository type command in the terminal:
$ git init
$ git add --all
$ git commit -m "Initial Commit"
2. Create a new repository.
Login into git repository
3. Locate git clone url
e.g. https://username@your..domain:7999 /yourproject/repo.git
4. Push the files to repository
type command in the terminal: $ git remote add origin https ://username@yourname.domain:7999/yourproject/repo.git $ git push -u origin master 5. Error - failed to push some refs to 'https://username@bitbucket.org/yourname/yourproject/repo.git
type command
$ git pull --rebase origin master
$ git push origin master
hat way, you would replay (the --rebase part) your local commits on top of the newly
updated origin/master (or origin/yourBranch: git pull origin yourBranch).--------------------------------------------------------------------------------------------------------------
1. Getting Help
Three
ways to get the manual page (manpage) help for any of the Git commands
$
git help
<verb>
$
git
<verb> --help
$ man git <verb>
e.g. $ git help config
2. Check Your Git Settings
$
git config –-list 3.
a. Check user name
To get user name for a specific repository
To get user name for a specific repository
$
git
config user.name Or $ git config --get user.name
Nitin
To get user name for all
repositories
$
git
config --global user.name Or $ git config --global --get user.name
Nitin
b. Check user email
To get user email address
for a specific repository
To get user email address
for all repositories
3. Your
Identity
Set user name & email address. This is important because every Git
commit uses this information
To set user name for a specific repository
a. $
git config user.name
“Nitin Karale”
To set username for every repository
a. $
git config
--global user.name “Nitin Karale”
b. $
git config
–global user.email nitin.tkarale@gmail.com
4. Check
remote url To
get remote url of repository $
git config --get remote.origin.urlhttps://nitin_karale@bitbucket.org/nitin_karale/sorghum.git
$
git remote
show origin
*
remote
origin Fetch
URL: https://username@domain/yourname/repo.git Push
URL: https://username@bitbucket.org/yourname/repo.git HEAD branch: master Remote branches:
SorghumV1 tracked
SorghumV2 tracked
google-play-services_lib tracked
master tracked
sorghum-android-studioV2 tracked Local branch configured for 'git pull':
SorghumV2 merges with remote SorghumV2 Local refs configured for 'git push':
SorghumV2 pushes to SorghumV2 (up to date)
sorghum-android-studioV2 pushes to sorghum-android-studioV2 (up to date)
5. Set remote url in initialize
a) Initialize git first
$
$
git
init
It shows initialize git
b) Check remote url by using following
command
$
git remote
–v
origin https://username@domain/yourname/repo.git (fetch)
origin
https://username@domain/yourname/repo.git (push)
If it is empty then use commandE.g. $
git
remote add origin {repository-url}
6. Make sure you have not set the
GIT_COMMITTER_NAME or GIT_AUTHOR_NAME variables. You can check their values
with the following command:
a. $ echo $GIT_COMMITTER_NAME
# prints the value of GIT_COMMITTER_NAME
Output - It’s NULL (if not set value)
b. $ echo $GIT_AUTHOR_NAME
# prints the value of GIT_COMMITTER_NAME
Output - It’s NULL (if not set value)
Set committer name &
author name
a. $ GIT_COMMITTER_NAME=Nitin
Karale
b. $ GIT_AUTHOR_NAME=Nitin Karale
7. Unstage
an added file in Git. If you added a file by mistake, you can unstage it (but
keep local changes) by saying
$ git reset HEAD path/to/file This is also what $
git status will
tell you
8. Merging
After pull it will get merging
HCDC-PC8+Nitin@HCDC-PC8 MINGW64 /e/NVLI/automated-classification-tool (dev|MERGING)
Then use following command
$ git commit
It shows git commit then use :q command
9. Clone specific branch
$ git clone –b {specific
branch} {repository url}e.g $ git clone –b dev {remote url}
10. Shows remote branches & local
branches
$ git branch * master
$ git branch -a
* master origin/1-2-stable origin/2-0-stable origin/2-1-stable origin/2-2-stable origin/3-0-unstable origin/HEAD origin/master $ git branch -r
origin/1-2-stable origin/2-0-stable origin/2-1-stable origin/2-2-stable origin/3-0-unstable origin/HEAD origin/master
11. Remove local untracked files from the
current Git branch
a) If you
want to see which files will be deleted you can use the -n option before you
run the actual command:
$ git clean –n
E.g.
$
$
git clean -n
Would remove
src/main/webapp/WEB-INF/glassfish-resources.xml
b)
Then when you are comfortable (because it will delete the files for
real!) use the -f option:$ git clean –f
c) Here are some more options for you to
delete directories, files, ignored and non-ignored files
· To remove directories, run $ git clean –f -d or $ git clean –fd
· To remove ignored files, run
$ git clean –f -X or $ git clean –fX
· To remove ignored and non-ignored files,
run
$ git clean –f -x or $ git clean –fx
12. Stash - To record the current state
of the working directory
a) To record the current state of the working directory and the index, but want to go back to a clean working directory
$ git stash
b)
To show list of
stash
$ git stash list stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
stash@{1}: On master: 9cc0589... Add git-stash
Or
$
git stash show Update git-stash documentation
Add git-stash
c) To pop stash
Remove a single stashed state from
the stash list and apply it on top of the current working tree state
$ git stash pop
d) To “Apply” Like pop, but do not
remove the state from the stash list
$ git stash apply
stash@{0}
e) “Clear” - Remove all the stash entries.
Note that those entries will then be subject to pruning, and may be impossible
to recover
$ git stash clear .gitignore Remove ignores files which are tracked before git add
a) Create a .gitignore file in project root
b) Edit .gitignore file in notepad
c) write all the files line by line you don´t want to add on the repository e.g. d)Then in your git bash you have to write the following line:
$ git config --global core.excludesfile ~/.gitignore_global
e) Commit all files $ git rm -r --cached .
$ git add .
$ git commit -m ".gitignore is now working"
Subscribe to:
Posts (Atom)
